ENV
is a hash-like accessor for environment variables.
The ENV
object interacts with the operating systemâs environment variables:
When you get the value for a name in ENV
, the value is retrieved from among the current environment variables.
When you create or set a name-value pair in ENV
, the name and value are immediately set in the environment variables.
When you delete a name-value pair in ENV
, it is immediately deleted from the environment variables.
Generally, a name or value is a String
.
Each name or value must be one of the following:
A String
.
An object that responds to #to_str by returning a String
, in which case that String
will be used as the name or value.
A new name:
May not be the empty string:
ENV[''] = '0'
May not contain character "="
:
ENV['='] = '0'
A new name or value:
May not be a non-String that does not respond to #to_str:
ENV['foo'] = Object.new ENV[Object.new] = '0'
May not contain the NUL character "\0"
:
ENV['foo'] = "\0" ENV["\0"] == '0'
May not have an ASCII-incompatible encoding such as UTF-16LE or ISO-2022-JP:
ENV['foo'] = '0'.force_encoding(Encoding::ISO_2022_JP) ENV["foo".force_encoding(Encoding::ISO_2022_JP)] = '0'
ENV
enumerates its name/value pairs in the order found in the operating systemâs environment variables. Therefore the ordering of ENV
content is OS-dependent, and may be indeterminate.
This will be seen in:
An Enumerator
returned by an ENV
method.
An Array
returned by ENV.keys
, ENV.values
, or ENV.to_a
.
The String
returned by ENV.inspect
.
The name returned by ENV.key
.
Some methods in ENV
return ENV
itself. Typically, there are many environment variables. Itâs not useful to display a large ENV
in the examples here, so most example snippets begin by resetting the contents of ENV
:
ENV.replace
replaces ENV
with a new collection of entries.
First, whatâs elsewhere. Class ENV
:
Inherits from class Object.
Extends module Enumerable,
Here, class ENV
provides methods that are useful for:
::[]
: Returns the value for the given environment variable name if it exists:
::has_value?
, ::value?
: Returns whether the given value is in ENV
.
::include?
, ::has_key?
, ::key?
, ::member?
: Returns whether the given name is in ENV
.
::key
: Returns the name of the first entry with the given value.
::value?
: Returns whether any entry has the given value.
::[]=
, ::store
: Creates, updates, or deletes the named environment variable.
::update
, ::merge!
: Adds to ENV
each key/value pair in the given hash.
::replace
: Replaces the entire content of the ENV
with the name/value pairs in the given hash.
::delete
: Deletes the named environment variable name if it exists.
::delete_if
: Deletes entries selected by the block.
::keep_if
: Deletes entries not selected by the block.
::reject!
: Similar to delete_if, but returns nil
if no change was made.
::select!
, ::filter!
: Deletes entries selected by the block.
::shift
: Removes and returns the first entry.
::each
, ::each_pair
: Calls the block with each name/value pair.
::each_key
: Calls the block with each name.
::each_value
: Calls the block with each value.
::assoc
: Returns a 2-element array containing the name and value of the named environment variable if it exists:
::except
: Returns a hash of all name/value pairs except those given.
::fetch
: Returns the value for the given name.
::invert
: Returns a hash whose keys are the ENV
values, and whose values are the corresponding ENV
names.
::keys
: Returns an array of all names.
::rassoc
: Returns the name and value of the first found entry that has the given value.
::reject
: Returns a hash of those entries not rejected by the block.
::select
, ::filter
: Returns a hash of name/value pairs selected by the block.
::slice
: Returns a hash of the given names and their corresponding values.
::to_a
: Returns the entries as an array of 2-element Arrays.
::to_h
: Returns a hash of entries selected by the block.
::to_hash
: Returns a hash of all entries.
::to_s
: Returns the string 'ENV'
.
::values
: Returns all values as an array.
::values_at
: Returns an array of the values for the given name.
static VALUE rb_f_getenv(VALUE obj, VALUE name) { const char *nam = env_name(name); VALUE env = getenv_with_lock(nam); return env; }
Returns the value for the environment variable name
if it exists:
ENV['foo'] = '0' ENV['foo']
Returns nil
if the named variable does not exist.
Raises an exception if name
is invalid. See Invalid Names and Values.
static VALUE env_aset_m(VALUE obj, VALUE nm, VALUE val) { return env_aset(nm, val); }
Creates, updates, or deletes the named environment variable, returning the value. Both name
and value
may be instances of String
. See Valid Names and Values.
If the named environment variable does not exist:
If value
is nil
, does nothing.
ENV.clear ENV['foo'] = nil ENV.include?('foo') ENV.store('bar', nil) ENV.include?('bar')
If value
is not nil
, creates the environment variable with name
and value
:
ENV['foo'] = '0' ENV['foo'] ENV.store('bar', '1') ENV['bar']
If the named environment variable exists:
If value
is not nil
, updates the environment variable with value value
:
ENV['foo'] = '2' ENV['foo'] ENV.store('bar', '3') ENV['bar']
If value
is nil
, deletes the environment variable:
ENV['foo'] = nil ENV.include?('foo') ENV.store('bar', nil) ENV.include?('bar')
Raises an exception if name
or value
is invalid. See Invalid Names and Values.
static VALUE env_assoc(VALUE env, VALUE key) { const char *s = env_name(key); VALUE e = getenv_with_lock(s); if (!NIL_P(e)) { return rb_assoc_new(key, e); } else { return Qnil; } }
Returns a 2-element Array
containing the name and value of the environment variable for name
if it exists:
ENV.replace('foo' => '0', 'bar' => '1') ENV.assoc('foo')
Returns nil
if name
is a valid String
and there is no such environment variable.
Returns nil
if name
is the empty String
or is a String
containing character '='
.
Raises an exception if name
is a String
containing the NUL character "\0"
:
ENV.assoc("\0")
Raises an exception if name
has an encoding that is not ASCII-compatible:
ENV.assoc("\xa1\xa1".force_encoding(Encoding::UTF_16LE))
Raises an exception if name
is not a String:
ENV.assoc(Object.new)Source
static VALUE env_clear(VALUE _) { return rb_env_clear(); }
Removes every environment variable; returns ENV:
ENV.replace('foo' => '0', 'bar' => '1') ENV.size ENV.clear ENV.sizeSource
static VALUE env_clone(int argc, VALUE *argv, VALUE obj) { if (argc) { VALUE opt; if (rb_scan_args(argc, argv, "0:", &opt) < argc) { rb_get_freeze_opt(1, &opt); } } rb_raise(rb_eTypeError, "Cannot clone ENV, use ENV.to_h to get a copy of ENV as a hash"); }
Raises TypeError
, because ENV
is a wrapper for the process-wide environment variables and a clone is useless. Use to_h to get a copy of ENV
data as a hash.
static VALUE env_delete_m(VALUE obj, VALUE name) { VALUE val; val = env_delete(name); if (NIL_P(val) && rb_block_given_p()) val = rb_yield(name); return val; }
Deletes the environment variable with name
if it exists and returns its value:
ENV['foo'] = '0' ENV.delete('foo')
If a block is not given and the named environment variable does not exist, returns nil
.
If a block given and the environment variable does not exist, yields name
to the block and returns the value of the block:
ENV.delete('foo') { |name| name * 2 }
If a block given and the environment variable exists, deletes the environment variable and returns its value (ignoring the block):
ENV['foo'] = '0' ENV.delete('foo') { |name| raise 'ignored' }
Raises an exception if name
is invalid. See Invalid Names and Values.
static VALUE env_delete_if(VALUE ehash) { RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size); env_reject_bang(ehash); return envtbl; }
Yields each environment variable name and its value as a 2-element Array
, deleting each environment variable for which the block returns a truthy value, and returning ENV
(regardless of whether any deletions):
ENV.replace('foo' => '0', 'bar' => '1', 'baz' => '2') ENV.delete_if { |name, value| name.start_with?('b') } ENV ENV.delete_if { |name, value| name.start_with?('b') }
Returns an Enumerator
if no block given:
ENV.replace('foo' => '0', 'bar' => '1', 'baz' => '2') e = ENV.delete_if e.each { |name, value| name.start_with?('b') } ENV e.each { |name, value| name.start_with?('b') }Source
static VALUE env_dup(VALUE obj) { rb_raise(rb_eTypeError, "Cannot dup ENV, use ENV.to_h to get a copy of ENV as a hash"); }
Raises TypeError
, because ENV
is a singleton object. Use to_h to get a copy of ENV
data as a hash.
static VALUE env_each_pair(VALUE ehash) { long i; RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size); VALUE ary = rb_ary_new(); ENV_LOCKING() { char **env = GET_ENVIRON(environ); while (*env) { char *s = strchr(*env, '='); if (s) { rb_ary_push(ary, env_str_new(*env, s-*env)); rb_ary_push(ary, env_str_new2(s+1)); } env++; } FREE_ENVIRON(environ); } if (rb_block_pair_yield_optimizable()) { for (i=0; i<RARRAY_LEN(ary); i+=2) { rb_yield_values(2, RARRAY_AREF(ary, i), RARRAY_AREF(ary, i+1)); } } else { for (i=0; i<RARRAY_LEN(ary); i+=2) { rb_yield(rb_assoc_new(RARRAY_AREF(ary, i), RARRAY_AREF(ary, i+1))); } } return ehash; }
Yields each environment variable name and its value as a 2-element Array:
h = {} ENV.each_pair { |name, value| h[name] = value } h
Returns an Enumerator
if no block given:
h = {} e = ENV.each_pair e.each { |name, value| h[name] = value } hSource
static VALUE env_each_key(VALUE ehash) { VALUE keys; long i; RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size); keys = env_keys(FALSE); for (i=0; i<RARRAY_LEN(keys); i++) { rb_yield(RARRAY_AREF(keys, i)); } return ehash; }
Yields each environment variable name:
ENV.replace('foo' => '0', 'bar' => '1') names = [] ENV.each_key { |name| names.push(name) } names
Returns an Enumerator
if no block given:
e = ENV.each_key names = [] e.each { |name| names.push(name) } namesSource
static VALUE env_each_pair(VALUE ehash) { long i; RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size); VALUE ary = rb_ary_new(); ENV_LOCKING() { char **env = GET_ENVIRON(environ); while (*env) { char *s = strchr(*env, '='); if (s) { rb_ary_push(ary, env_str_new(*env, s-*env)); rb_ary_push(ary, env_str_new2(s+1)); } env++; } FREE_ENVIRON(environ); } if (rb_block_pair_yield_optimizable()) { for (i=0; i<RARRAY_LEN(ary); i+=2) { rb_yield_values(2, RARRAY_AREF(ary, i), RARRAY_AREF(ary, i+1)); } } else { for (i=0; i<RARRAY_LEN(ary); i+=2) { rb_yield(rb_assoc_new(RARRAY_AREF(ary, i), RARRAY_AREF(ary, i+1))); } } return ehash; }
Yields each environment variable name and its value as a 2-element Array:
h = {} ENV.each_pair { |name, value| h[name] = value } h
Returns an Enumerator
if no block given:
h = {} e = ENV.each_pair e.each { |name, value| h[name] = value } hSource
static VALUE env_each_value(VALUE ehash) { VALUE values; long i; RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size); values = env_values(); for (i=0; i<RARRAY_LEN(values); i++) { rb_yield(RARRAY_AREF(values, i)); } return ehash; }
Yields each environment variable value:
ENV.replace('foo' => '0', 'bar' => '1') values = [] ENV.each_value { |value| values.push(value) } values
Returns an Enumerator
if no block given:
e = ENV.each_value values = [] e.each { |value| values.push(value) } valuesSource
static VALUE env_empty_p(VALUE _) { bool empty = true; ENV_LOCKING() { char **env = GET_ENVIRON(environ); if (env[0] != 0) { empty = false; } FREE_ENVIRON(environ); } return RBOOL(empty); }
Returns true
when there are no environment variables, false
otherwise:
ENV.clear ENV.empty? ENV['foo'] = '0' ENV.empty?Source
static VALUE env_except(int argc, VALUE *argv, VALUE _) { int i; VALUE key, hash = env_to_hash(); for (i = 0; i < argc; i++) { key = argv[i]; rb_hash_delete(hash, key); } return hash; }
Returns a hash except the given keys from ENV
and their values.
ENV ENV.except("TERM","HOME")Source
static VALUE env_fetch(int argc, VALUE *argv, VALUE _) { VALUE key; long block_given; const char *nam; VALUE env; rb_check_arity(argc, 1, 2); key = argv[0]; block_given = rb_block_given_p(); if (block_given && argc == 2) { rb_warn("block supersedes default value argument"); } nam = env_name(key); env = getenv_with_lock(nam); if (NIL_P(env)) { if (block_given) return rb_yield(key); if (argc == 1) { rb_key_err_raise(rb_sprintf("key not found: \"%"PRIsVALUE"\"", key), envtbl, key); } return argv[1]; } return env; }
If name
is the name of an environment variable, returns its value:
ENV['foo'] = '0' ENV.fetch('foo')
Otherwise if a block is given (but not a default value), yields name
to the block and returns the blockâs return value:
ENV.fetch('foo') { |name| :need_not_return_a_string }
Otherwise if a default value is given (but not a block), returns the default value:
ENV.delete('foo') ENV.fetch('foo', :default_need_not_be_a_string)
If the environment variable does not exist and both default and block are given, issues a warning (âwarning: block supersedes default value argumentâ), yields name
to the block, and returns the blockâs return value:
ENV.fetch('foo', :default) { |name| :block_return }
Raises KeyError
if name
is valid, but not found, and neither default value nor block is given:
ENV.fetch('foo')
Raises an exception if name
is invalid. See Invalid Names and Values.
static VALUE env_select(VALUE ehash) { VALUE result; VALUE keys; long i; RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size); result = rb_hash_new(); keys = env_keys(FALSE); for (i = 0; i < RARRAY_LEN(keys); ++i) { VALUE key = RARRAY_AREF(keys, i); VALUE val = rb_f_getenv(Qnil, key); if (!NIL_P(val)) { if (RTEST(rb_yield_values(2, key, val))) { rb_hash_aset(result, key, val); } } } RB_GC_GUARD(keys); return result; }
Yields each environment variable name and its value as a 2-element Array
, returning a Hash
of the names and values for which the block returns a truthy value:
ENV.replace('foo' => '0', 'bar' => '1', 'baz' => '2') ENV.select { |name, value| name.start_with?('b') } ENV.filter { |name, value| name.start_with?('b') }
Returns an Enumerator
if no block given:
e = ENV.select e.each { |name, value | name.start_with?('b') } e = ENV.filter e.each { |name, value | name.start_with?('b') }Source
static VALUE env_select_bang(VALUE ehash) { VALUE keys; long i; int del = 0; RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size); keys = env_keys(FALSE); RBASIC_CLEAR_CLASS(keys); for (i=0; i<RARRAY_LEN(keys); i++) { VALUE val = rb_f_getenv(Qnil, RARRAY_AREF(keys, i)); if (!NIL_P(val)) { if (!RTEST(rb_yield_values(2, RARRAY_AREF(keys, i), val))) { env_delete(RARRAY_AREF(keys, i)); del++; } } } RB_GC_GUARD(keys); if (del == 0) return Qnil; return envtbl; }
Yields each environment variable name and its value as a 2-element Array
, deleting each entry for which the block returns false
or nil
, and returning ENV
if any deletions made, or nil
otherwise:
ENV.replace('foo' => '0', 'bar' => '1', 'baz' => '2') ENV.select! { |name, value| name.start_with?('b') } ENV ENV.select! { |name, value| true } ENV.replace('foo' => '0', 'bar' => '1', 'baz' => '2') ENV.filter! { |name, value| name.start_with?('b') } ENV ENV.filter! { |name, value| true }
Returns an Enumerator
if no block given:
ENV.replace('foo' => '0', 'bar' => '1', 'baz' => '2') e = ENV.select! e.each { |name, value| name.start_with?('b') } ENV e.each { |name, value| true } ENV.replace('foo' => '0', 'bar' => '1', 'baz' => '2') e = ENV.filter! e.each { |name, value| name.start_with?('b') } ENV e.each { |name, value| true }Source
static VALUE env_freeze(VALUE self) { rb_raise(rb_eTypeError, "cannot freeze ENV"); UNREACHABLE_RETURN(self); }
Raises an exception:
ENV.freezeSource
static VALUE env_has_key(VALUE env, VALUE key) { const char *s = env_name(key); return RBOOL(has_env_with_lock(s)); }
Returns true
if there is an environment variable with the given name
:
ENV.replace('foo' => '0', 'bar' => '1') ENV.include?('foo')
Returns false
if name
is a valid String
and there is no such environment variable:
ENV.include?('baz')
Returns false
if name
is the empty String
or is a String
containing character '='
:
ENV.include?('') ENV.include?('=')
Raises an exception if name
is a String
containing the NUL character "\0"
:
ENV.include?("\0")
Raises an exception if name
has an encoding that is not ASCII-compatible:
ENV.include?("\xa1\xa1".force_encoding(Encoding::UTF_16LE))
Raises an exception if name
is not a String:
ENV.include?(Object.new)Source
static VALUE env_has_value(VALUE dmy, VALUE obj) { obj = rb_check_string_type(obj); if (NIL_P(obj)) return Qnil; VALUE ret = Qfalse; ENV_LOCKING() { char **env = GET_ENVIRON(environ); while (*env) { char *s = strchr(*env, '='); if (s++) { long len = strlen(s); if (RSTRING_LEN(obj) == len && strncmp(s, RSTRING_PTR(obj), len) == 0) { ret = Qtrue; break; } } env++; } FREE_ENVIRON(environ); } return ret; }
Returns true
if value
is the value for some environment variable name, false
otherwise:
ENV.replace('foo' => '0', 'bar' => '1') ENV.value?('0') ENV.has_value?('0') ENV.value?('2') ENV.has_value?('2')Source
static VALUE env_has_key(VALUE env, VALUE key) { const char *s = env_name(key); return RBOOL(has_env_with_lock(s)); }
Returns true
if there is an environment variable with the given name
:
ENV.replace('foo' => '0', 'bar' => '1') ENV.include?('foo')
Returns false
if name
is a valid String
and there is no such environment variable:
ENV.include?('baz')
Returns false
if name
is the empty String
or is a String
containing character '='
:
ENV.include?('') ENV.include?('=')
Raises an exception if name
is a String
containing the NUL character "\0"
:
ENV.include?("\0")
Raises an exception if name
has an encoding that is not ASCII-compatible:
ENV.include?("\xa1\xa1".force_encoding(Encoding::UTF_16LE))
Raises an exception if name
is not a String:
ENV.include?(Object.new)Source
static VALUE env_inspect(VALUE _) { VALUE str = rb_str_buf_new2("{"); rb_encoding *enc = env_encoding(); ENV_LOCKING() { char **env = GET_ENVIRON(environ); while (*env) { const char *s = strchr(*env, '='); if (env != environ) { rb_str_buf_cat2(str, ", "); } if (s) { rb_str_buf_append(str, rb_str_inspect(env_enc_str_new(*env, s-*env, enc))); rb_str_buf_cat2(str, " => "); s++; rb_str_buf_append(str, rb_str_inspect(env_enc_str_new(s, strlen(s), enc))); } env++; } FREE_ENVIRON(environ); } rb_str_buf_cat2(str, "}"); return str; }
Returns the contents of the environment as a String:
ENV.replace('foo' => '0', 'bar' => '1') ENV.inspectSource
static VALUE env_invert(VALUE _) { return rb_hash_invert(env_to_hash()); }
Returns a Hash
whose keys are the ENV
values, and whose values are the corresponding ENV
names:
ENV.replace('foo' => '0', 'bar' => '1') ENV.invert
For a duplicate ENV
value, overwrites the hash entry:
ENV.replace('foo' => '0', 'bar' => '0') ENV.invert
Note that the order of the ENV
processing is OS-dependent, which means that the order of overwriting is also OS-dependent. See About Ordering.
static VALUE env_keep_if(VALUE ehash) { RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size); env_select_bang(ehash); return envtbl; }
Yields each environment variable name and its value as a 2-element Array
, deleting each environment variable for which the block returns false
or nil
, and returning ENV:
ENV.replace('foo' => '0', 'bar' => '1', 'baz' => '2') ENV.keep_if { |name, value| name.start_with?('b') } ENV
Returns an Enumerator
if no block given:
ENV.replace('foo' => '0', 'bar' => '1', 'baz' => '2') e = ENV.keep_if e.each { |name, value| name.start_with?('b') } ENVSource
static VALUE env_key(VALUE dmy, VALUE value) { StringValue(value); VALUE str = Qnil; ENV_LOCKING() { char **env = GET_ENVIRON(environ); while (*env) { char *s = strchr(*env, '='); if (s++) { long len = strlen(s); if (RSTRING_LEN(value) == len && strncmp(s, RSTRING_PTR(value), len) == 0) { str = env_str_new(*env, s-*env-1); break; } } env++; } FREE_ENVIRON(environ); } return str; }
Returns the name of the first environment variable with value
, if it exists:
ENV.replace('foo' => '0', 'bar' => '0') ENV.key('0')
The order in which environment variables are examined is OS-dependent. See About Ordering.
Returns nil
if there is no such value.
Raises an exception if value
is invalid:
ENV.key(Object.new)Source
static VALUE env_has_key(VALUE env, VALUE key) { const char *s = env_name(key); return RBOOL(has_env_with_lock(s)); }
Returns true
if there is an environment variable with the given name
:
ENV.replace('foo' => '0', 'bar' => '1') ENV.include?('foo')
Returns false
if name
is a valid String
and there is no such environment variable:
ENV.include?('baz')
Returns false
if name
is the empty String
or is a String
containing character '='
:
ENV.include?('') ENV.include?('=')
Raises an exception if name
is a String
containing the NUL character "\0"
:
ENV.include?("\0")
Raises an exception if name
has an encoding that is not ASCII-compatible:
ENV.include?("\xa1\xa1".force_encoding(Encoding::UTF_16LE))
Raises an exception if name
is not a String:
ENV.include?(Object.new)Source
static VALUE env_f_keys(VALUE _) { return env_keys(FALSE); }
Returns all variable names in an Array:
ENV.replace('foo' => '0', 'bar' => '1') ENV.keys
The order of the names is OS-dependent. See About Ordering.
Returns the empty Array
if ENV
is empty.
static VALUE env_size(VALUE _) { return INT2FIX(env_size_with_lock()); }
Returns the count of environment variables:
ENV.replace('foo' => '0', 'bar' => '1') ENV.length ENV.sizeSource
static VALUE env_has_key(VALUE env, VALUE key) { const char *s = env_name(key); return RBOOL(has_env_with_lock(s)); }
Returns true
if there is an environment variable with the given name
:
ENV.replace('foo' => '0', 'bar' => '1') ENV.include?('foo')
Returns false
if name
is a valid String
and there is no such environment variable:
ENV.include?('baz')
Returns false
if name
is the empty String
or is a String
containing character '='
:
ENV.include?('') ENV.include?('=')
Raises an exception if name
is a String
containing the NUL character "\0"
:
ENV.include?("\0")
Raises an exception if name
has an encoding that is not ASCII-compatible:
ENV.include?("\xa1\xa1".force_encoding(Encoding::UTF_16LE))
Raises an exception if name
is not a String:
ENV.include?(Object.new)Source
static VALUE env_update(int argc, VALUE *argv, VALUE env) { rb_foreach_func *func = rb_block_given_p() ? env_update_block_i : env_update_i; for (int i = 0; i < argc; ++i) { VALUE hash = argv[i]; if (env == hash) continue; hash = to_hash(hash); rb_hash_foreach(hash, func, 0); } return env; }
Adds to ENV
each key/value pair in the given hash
; returns ENV:
ENV.replace('foo' => '0', 'bar' => '1') ENV.merge!('baz' => '2', 'bat' => '3')
Deletes the ENV
entry for a hash value that is nil
:
ENV.merge!('baz' => nil, 'bat' => nil)
For an already-existing name, if no block given, overwrites the ENV
value:
ENV.merge!('foo' => '4')
For an already-existing name, if block given, yields the name, its ENV
value, and its hash value; the blockâs return value becomes the new name:
ENV.merge!('foo' => '5') { |name, env_val, hash_val | env_val + hash_val }
Raises an exception if a name or value is invalid (see Invalid Names and Values);
ENV.replace('foo' => '0', 'bar' => '1') ENV.merge!('foo' => '6', :bar => '7', 'baz' => '9') ENV ENV.merge!('foo' => '7', 'bar' => 8, 'baz' => '9') ENV
Raises an exception if the block returns an invalid name: (see Invalid Names and Values):
ENV.merge!('bat' => '8', 'foo' => '9') { |name, env_val, hash_val | 10 } ENV
Note that for the exceptions above, hash pairs preceding an invalid name or value are processed normally; those following are ignored.
Sourcestatic VALUE env_rassoc(VALUE dmy, VALUE obj) { obj = rb_check_string_type(obj); if (NIL_P(obj)) return Qnil; VALUE result = Qnil; ENV_LOCKING() { char **env = GET_ENVIRON(environ); while (*env) { const char *p = *env; char *s = strchr(p, '='); if (s++) { long len = strlen(s); if (RSTRING_LEN(obj) == len && strncmp(s, RSTRING_PTR(obj), len) == 0) { result = rb_assoc_new(rb_str_new(p, s-p-1), obj); break; } } env++; } FREE_ENVIRON(environ); } return result; }
Returns a 2-element Array
containing the name and value of the first found environment variable that has value value
, if one exists:
ENV.replace('foo' => '0', 'bar' => '0') ENV.rassoc('0')
The order in which environment variables are examined is OS-dependent. See About Ordering.
Returns nil
if there is no such environment variable.
static VALUE env_none(VALUE _) { return Qnil; }
(Provided for compatibility with Hash
.)
Does not modify ENV
; returns nil
.
static VALUE env_reject(VALUE _) { return rb_hash_delete_if(env_to_hash()); }
Yields each environment variable name and its value as a 2-element Array
. Returns a Hash
whose items are determined by the block. When the block returns a truthy value, the name/value pair is added to the return Hash
; otherwise the pair is ignored:
ENV.replace('foo' => '0', 'bar' => '1', 'baz' => '2') ENV.reject { |name, value| name.start_with?('b') }
Returns an Enumerator
if no block given:
e = ENV.reject e.each { |name, value| name.start_with?('b') }Source
static VALUE env_reject_bang(VALUE ehash) { VALUE keys; long i; int del = 0; RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size); keys = env_keys(FALSE); RBASIC_CLEAR_CLASS(keys); for (i=0; i<RARRAY_LEN(keys); i++) { VALUE val = rb_f_getenv(Qnil, RARRAY_AREF(keys, i)); if (!NIL_P(val)) { if (RTEST(rb_yield_values(2, RARRAY_AREF(keys, i), val))) { env_delete(RARRAY_AREF(keys, i)); del++; } } } RB_GC_GUARD(keys); if (del == 0) return Qnil; return envtbl; }
Similar to ENV.delete_if
, but returns nil
if no changes were made.
Yields each environment variable name and its value as a 2-element Array
, deleting each environment variable for which the block returns a truthy value, and returning ENV
(if any deletions) or nil
(if not):
ENV.replace('foo' => '0', 'bar' => '1', 'baz' => '2') ENV.reject! { |name, value| name.start_with?('b') } ENV ENV.reject! { |name, value| name.start_with?('b') }
Returns an Enumerator
if no block given:
ENV.replace('foo' => '0', 'bar' => '1', 'baz' => '2') e = ENV.reject! e.each { |name, value| name.start_with?('b') } ENV e.each { |name, value| name.start_with?('b') }Source
static VALUE env_replace(VALUE env, VALUE hash) { VALUE keys; long i; keys = env_keys(TRUE); if (env == hash) return env; hash = to_hash(hash); rb_hash_foreach(hash, env_replace_i, keys); for (i=0; i<RARRAY_LEN(keys); i++) { env_delete(RARRAY_AREF(keys, i)); } RB_GC_GUARD(keys); return env; }
Replaces the entire content of the environment variables with the name/value pairs in the given hash
; returns ENV
.
Replaces the content of ENV
with the given pairs:
ENV.replace('foo' => '0', 'bar' => '1') ENV.to_hash
Raises an exception if a name or value is invalid (see Invalid Names and Values):
ENV.replace('foo' => '0', :bar => '1') ENV.replace('foo' => '0', 'bar' => 1) ENV.to_hashSource
static VALUE env_select(VALUE ehash) { VALUE result; VALUE keys; long i; RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size); result = rb_hash_new(); keys = env_keys(FALSE); for (i = 0; i < RARRAY_LEN(keys); ++i) { VALUE key = RARRAY_AREF(keys, i); VALUE val = rb_f_getenv(Qnil, key); if (!NIL_P(val)) { if (RTEST(rb_yield_values(2, key, val))) { rb_hash_aset(result, key, val); } } } RB_GC_GUARD(keys); return result; }
Yields each environment variable name and its value as a 2-element Array
, returning a Hash
of the names and values for which the block returns a truthy value:
ENV.replace('foo' => '0', 'bar' => '1', 'baz' => '2') ENV.select { |name, value| name.start_with?('b') } ENV.filter { |name, value| name.start_with?('b') }
Returns an Enumerator
if no block given:
e = ENV.select e.each { |name, value | name.start_with?('b') } e = ENV.filter e.each { |name, value | name.start_with?('b') }Source
static VALUE env_select_bang(VALUE ehash) { VALUE keys; long i; int del = 0; RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size); keys = env_keys(FALSE); RBASIC_CLEAR_CLASS(keys); for (i=0; i<RARRAY_LEN(keys); i++) { VALUE val = rb_f_getenv(Qnil, RARRAY_AREF(keys, i)); if (!NIL_P(val)) { if (!RTEST(rb_yield_values(2, RARRAY_AREF(keys, i), val))) { env_delete(RARRAY_AREF(keys, i)); del++; } } } RB_GC_GUARD(keys); if (del == 0) return Qnil; return envtbl; }
Yields each environment variable name and its value as a 2-element Array
, deleting each entry for which the block returns false
or nil
, and returning ENV
if any deletions made, or nil
otherwise:
ENV.replace('foo' => '0', 'bar' => '1', 'baz' => '2') ENV.select! { |name, value| name.start_with?('b') } ENV ENV.select! { |name, value| true } ENV.replace('foo' => '0', 'bar' => '1', 'baz' => '2') ENV.filter! { |name, value| name.start_with?('b') } ENV ENV.filter! { |name, value| true }
Returns an Enumerator
if no block given:
ENV.replace('foo' => '0', 'bar' => '1', 'baz' => '2') e = ENV.select! e.each { |name, value| name.start_with?('b') } ENV e.each { |name, value| true } ENV.replace('foo' => '0', 'bar' => '1', 'baz' => '2') e = ENV.filter! e.each { |name, value| name.start_with?('b') } ENV e.each { |name, value| true }Source
static VALUE env_shift(VALUE _) { VALUE result = Qnil; VALUE key = Qnil; ENV_LOCKING() { char **env = GET_ENVIRON(environ); if (*env) { const char *p = *env; char *s = strchr(p, '='); if (s) { key = env_str_new(p, s-p); VALUE val = env_str_new2(getenv(RSTRING_PTR(key))); result = rb_assoc_new(key, val); } } FREE_ENVIRON(environ); } if (!NIL_P(key)) { env_delete(key); } return result; }
Removes the first environment variable from ENV
and returns a 2-element Array
containing its name and value:
ENV.replace('foo' => '0', 'bar' => '1') ENV.to_hash ENV.shift ENV.to_hash
Exactly which environment variable is âfirstâ is OS-dependent. See About Ordering.
Returns nil
if the environment is empty.
static VALUE env_size(VALUE _) { return INT2FIX(env_size_with_lock()); }
Returns the count of environment variables:
ENV.replace('foo' => '0', 'bar' => '1') ENV.length ENV.sizeSource
static VALUE env_slice(int argc, VALUE *argv, VALUE _) { int i; VALUE key, value, result; if (argc == 0) { return rb_hash_new(); } result = rb_hash_new_with_size(argc); for (i = 0; i < argc; i++) { key = argv[i]; value = rb_f_getenv(Qnil, key); if (value != Qnil) rb_hash_aset(result, key, value); } return result; }
Returns a Hash
of the given ENV
names and their corresponding values:
ENV.replace('foo' => '0', 'bar' => '1', 'baz' => '2', 'bat' => '3') ENV.slice('foo', 'baz') ENV.slice('baz', 'foo')
Raises an exception if any of the names
is invalid (see Invalid Names and Values):
ENV.slice('foo', 'bar', :bat)Source
static VALUE env_aset_m(VALUE obj, VALUE nm, VALUE val) { return env_aset(nm, val); }
Creates, updates, or deletes the named environment variable, returning the value. Both name
and value
may be instances of String
. See Valid Names and Values.
If the named environment variable does not exist:
If value
is nil
, does nothing.
ENV.clear ENV['foo'] = nil ENV.include?('foo') ENV.store('bar', nil) ENV.include?('bar')
If value
is not nil
, creates the environment variable with name
and value
:
ENV['foo'] = '0' ENV['foo'] ENV.store('bar', '1') ENV['bar']
If the named environment variable exists:
If value
is not nil
, updates the environment variable with value value
:
ENV['foo'] = '2' ENV['foo'] ENV.store('bar', '3') ENV['bar']
If value
is nil
, deletes the environment variable:
ENV['foo'] = nil ENV.include?('foo') ENV.store('bar', nil) ENV.include?('bar')
Raises an exception if name
or value
is invalid. See Invalid Names and Values.
static VALUE env_to_a(VALUE _) { VALUE ary = rb_ary_new(); ENV_LOCKING() { char **env = GET_ENVIRON(environ); while (*env) { char *s = strchr(*env, '='); if (s) { rb_ary_push(ary, rb_assoc_new(env_str_new(*env, s-*env), env_str_new2(s+1))); } env++; } FREE_ENVIRON(environ); } return ary; }
Returns the contents of ENV
as an Array
of 2-element Arrays, each of which is a name/value pair:
ENV.replace('foo' => '0', 'bar' => '1') ENV.to_aSource
static VALUE env_to_h(VALUE _) { VALUE hash = env_to_hash(); if (rb_block_given_p()) { hash = rb_hash_to_h_block(hash); } return hash; }
With no block, returns a Hash
containing all name/value pairs from ENV:
ENV.replace('foo' => '0', 'bar' => '1') ENV.to_h
With a block, returns a Hash
whose items are determined by the block. Each name/value pair in ENV
is yielded to the block. The block must return a 2-element Array
(name/value pair) that is added to the return Hash
as a key and value:
ENV.to_h { |name, value| [name.to_sym, value.to_i] }
Raises an exception if the block does not return an Array:
ENV.to_h { |name, value| name }
Raises an exception if the block returns an Array
of the wrong size:
ENV.to_h { |name, value| [name] }Source
static VALUE env_f_to_hash(VALUE _) { return env_to_hash(); }
Returns a Hash
containing all name/value pairs from ENV:
ENV.replace('foo' => '0', 'bar' => '1') ENV.to_hashSource
static VALUE env_to_s(VALUE _) { return rb_usascii_str_new2("ENV"); }
Returns String
âENVâ:
ENV.to_sSource
static VALUE env_update(int argc, VALUE *argv, VALUE env) { rb_foreach_func *func = rb_block_given_p() ? env_update_block_i : env_update_i; for (int i = 0; i < argc; ++i) { VALUE hash = argv[i]; if (env == hash) continue; hash = to_hash(hash); rb_hash_foreach(hash, func, 0); } return env; }
Adds to ENV
each key/value pair in the given hash
; returns ENV:
ENV.replace('foo' => '0', 'bar' => '1') ENV.merge!('baz' => '2', 'bat' => '3')
Deletes the ENV
entry for a hash value that is nil
:
ENV.merge!('baz' => nil, 'bat' => nil)
For an already-existing name, if no block given, overwrites the ENV
value:
ENV.merge!('foo' => '4')
For an already-existing name, if block given, yields the name, its ENV
value, and its hash value; the blockâs return value becomes the new name:
ENV.merge!('foo' => '5') { |name, env_val, hash_val | env_val + hash_val }
Raises an exception if a name or value is invalid (see Invalid Names and Values);
ENV.replace('foo' => '0', 'bar' => '1') ENV.merge!('foo' => '6', :bar => '7', 'baz' => '9') ENV ENV.merge!('foo' => '7', 'bar' => 8, 'baz' => '9') ENV
Raises an exception if the block returns an invalid name: (see Invalid Names and Values):
ENV.merge!('bat' => '8', 'foo' => '9') { |name, env_val, hash_val | 10 } ENV
Note that for the exceptions above, hash pairs preceding an invalid name or value are processed normally; those following are ignored.
Sourcestatic VALUE env_has_value(VALUE dmy, VALUE obj) { obj = rb_check_string_type(obj); if (NIL_P(obj)) return Qnil; VALUE ret = Qfalse; ENV_LOCKING() { char **env = GET_ENVIRON(environ); while (*env) { char *s = strchr(*env, '='); if (s++) { long len = strlen(s); if (RSTRING_LEN(obj) == len && strncmp(s, RSTRING_PTR(obj), len) == 0) { ret = Qtrue; break; } } env++; } FREE_ENVIRON(environ); } return ret; }
Returns true
if value
is the value for some environment variable name, false
otherwise:
ENV.replace('foo' => '0', 'bar' => '1') ENV.value?('0') ENV.has_value?('0') ENV.value?('2') ENV.has_value?('2')Source
static VALUE env_f_values(VALUE _) { return env_values(); }
Returns all environment variable values in an Array:
ENV.replace('foo' => '0', 'bar' => '1') ENV.values
The order of the values is OS-dependent. See About Ordering.
Returns the empty Array
if ENV
is empty.
static VALUE env_values_at(int argc, VALUE *argv, VALUE _) { VALUE result; long i; result = rb_ary_new(); for (i=0; i<argc; i++) { rb_ary_push(result, rb_f_getenv(Qnil, argv[i])); } return result; }
Returns an Array
containing the environment variable values associated with the given names:
ENV.replace('foo' => '0', 'bar' => '1', 'baz' => '2') ENV.values_at('foo', 'baz')
Returns nil
in the Array
for each name that is not an ENV
name:
ENV.values_at('foo', 'bat', 'bar', 'bam')
Returns an empty Array
if no names given.
Raises an exception if any name is invalid. See Invalid Names and Values.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4