Class: ENV
Relationships & Source Files | |
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
self,
::Enumerable
|
|
Inherits: | Object |
Defined in: | hash.c, hash.c |
Overview
ENV
is a hash-like accessor for environment variables.
Class Attribute Summary
-
.empty? ⇒ Boolean
readonly
Returns true when there are no environment variables.
Class Method Summary
-
.[](name) ⇒ value
Retrieves the
value
for environment variablename
as a::String
. -
.[]=(name, value)
(also: .store)
Sets the environment variable
name
tovalue
. -
.assoc(name) ⇒ Array?
Returns an
::Array
of the name and value of the environment variable withname
ornil
if the name cannot be found. -
.clear
Removes every environment variable.
-
.delete(name) ⇒ value
Deletes the environment variable with
name
and returns the value of the variable. -
.delete_if {|name, value| ... } ⇒ Hash
Deletes every environment variable for which the block evaluates to
true
. -
.each {|name, value| ... } ⇒ Hash
(also: .each_pair)
Yields each environment variable
name
andvalue
. -
.each_key {|name| ... } ⇒ Hash
Yields each environment variable name.
-
.each_pair {|name, value| ... } ⇒ Hash
Alias for .each.
-
.each_value {|value| ... } ⇒ Hash
Yields each environment variable
value
. -
.fetch(name) ⇒ value
Retrieves the environment variable
name
. -
.filter {|name, value| ... } ⇒ Hash
(also: .select)
Returns a copy of the environment for entries where the block returns true.
-
.filter! {|name, value| ... } ⇒ ENV?
(also: .select!)
Equivalent to .keep_if but returns
nil
if no changes were made. -
.has_key?(name) ⇒ Boolean
Alias for .key?.
-
.has_value?(value) ⇒ Boolean
Alias for .value?.
-
.include?(name) ⇒ Boolean
Alias for .key?.
-
.index(value) ⇒ key
Deprecated method that is equivalent to .key
-
.inspect ⇒ String
Returns the contents of the environment as a
::String
. -
.invert ⇒ Hash
Returns a new hash created by using environment variable names as values and values as names.
-
.keep_if {|name, value| ... } ⇒ Hash
Deletes every environment variable where the block evaluates to
false
. -
.key(value) ⇒ name
Returns the name of the environment variable with
value
. -
.key?(name) ⇒ Boolean
(also: .include?, .member?, .has_key?)
Returns
true
if there is an environment variable with the givenname
. -
.keys ⇒ Array
Returns every environment variable name in an
::Array
. -
.length
(also: .size)
Returns the number of environment variables.
-
.member?(name) ⇒ Boolean
Alias for .key?.
-
.rassoc(value)
Returns an
::Array
of the name and value of the environment variable withvalue
ornil
if the value cannot be found. -
.rehash
Re-hashing the environment variables does nothing.
-
.reject {|name, value| ... } ⇒ Hash
Same as .delete_if, but works on (and returns) a copy of the environment.
-
.reject! {|name, value| ... } ⇒ ENV?
Equivalent to .delete_if but returns
nil
if no changes were made. -
.replace(hash) ⇒ ENV
Replaces the contents of the environment variables with the contents of
hash
. -
.select {|name, value| ... } ⇒ Hash
Alias for .filter.
-
.select! {|name, value| ... } ⇒ ENV?
Alias for .filter!.
-
.shift ⇒ Array?
Removes an environment variable name-value pair from
ENV
and returns it as an::Array
. -
.size
Alias for .length.
-
.slice(*keys) ⇒ Hash
Returns a hash containing only the given keys from
ENV
and their values. -
.store(name, value) ⇒ value
Alias for .[]=.
-
.to_a ⇒ Array
Converts the environment variables into an array of names and value arrays.
-
.to_h ⇒ Hash
Creates a hash with a copy of the environment variables.
-
.to_hash ⇒ Hash
Creates a hash with a copy of the environment variables.
-
.to_s ⇒ ENV
Returns “ENV”.
-
.update(hash) ⇒ Hash
Adds the contents of
hash
to the environment variables. -
.value?(value) ⇒ Boolean
(also: .has_value?)
Returns
true
if there is an environment variable with the givenvalue
. -
.values ⇒ Array
Returns every environment variable value as an
::Array
. -
.values_at(name, ...) ⇒ Array
Returns an array containing the environment variable values associated with the given names.
::Enumerable
- Extended
all? | Passes each element of the collection to the given block. |
any? | Passes each element of the collection to the given block. |
chain | Returns an enumerator object generated from this enumerator and given enumerables. |
chunk | Enumerates over the items, chunking them together based on the return value of the block. |
chunk_while | Creates an enumerator for each chunked elements. |
collect | Alias for Enumerable#map. |
collect_concat | Alias for Enumerable#flat_map. |
count | Returns the number of items in |
cycle | Calls block for each element of enum repeatedly n times or forever if none or |
detect | Alias for Enumerable#find. |
drop | Drops first n elements from enum, and returns rest elements in an array. |
drop_while | Drops elements up to, but not including, the first element for which the block returns |
each_cons | Iterates the given block for each array of consecutive <n> elements. |
each_entry | Calls block once for each element in |
each_slice | Iterates the given block for each slice of <n> elements. |
each_with_index | Calls block with two arguments, the item and its index, for each item in enum. |
each_with_object | Iterates the given block for each element with an arbitrary object given, and returns the initially given object. |
entries | Alias for Enumerable#to_a. |
filter | Returns an array containing all elements of |
find | Passes each entry in enum to block. |
find_all | Alias for Enumerable#filter. |
find_index | Compares each entry in enum with value or passes to block. |
first | Returns the first element, or the first |
flat_map | Returns a new array with the concatenated results of running block once for every element in enum. |
grep | Returns an array of every element in enum for which |
grep_v | Inverted version of Enumerable#grep. |
group_by | Groups the collection by result of the block. |
include? | Alias for Enumerable#member?. |
inject | Combines all elements of enum by applying a binary operation, specified by a block or a symbol that names a method or operator. |
lazy | Returns a lazy enumerator, whose methods map/collect, flat_map/collect_concat, select/find_all, reject, grep, grep_v, zip, take, take_while, drop, and drop_while enumerate values only on an as-needed basis. |
map | Returns a new array with the results of running block once for every element in enum. |
max | Returns the object in enum with the maximum value. |
max_by | Returns the object in enum that gives the maximum value from the given block. |
member? | Returns |
min | Returns the object in enum with the minimum value. |
min_by | Returns the object in enum that gives the minimum value from the given block. |
minmax | Returns a two element array which contains the minimum and the maximum value in the enumerable. |
minmax_by | Returns a two element array containing the objects in enum that correspond to the minimum and maximum values respectively from the given block. |
none? | Passes each element of the collection to the given block. |
one? | Passes each element of the collection to the given block. |
partition | Returns two arrays, the first containing the elements of enum for which the block evaluates to true, the second containing the rest. |
reduce | Alias for Enumerable#inject. |
reject | Returns an array for all elements of |
reverse_each | Builds a temporary array and traverses that array in reverse order. |
select | Alias for Enumerable#filter. |
slice_after | Creates an enumerator for each chunked elements. |
slice_before | Creates an enumerator for each chunked elements. |
slice_when | Creates an enumerator for each chunked elements. |
sort | Returns an array containing the items in enum sorted. |
sort_by | Sorts enum using a set of keys generated by mapping the values in enum through the given block. |
sum | Returns the sum of elements in an |
take | Returns first n elements from enum. |
take_while | Passes elements to the block until the block returns |
to_a | Returns an array containing the items in enum. |
to_h | Returns the result of interpreting enum as a list of |
uniq | Returns a new array by removing duplicate values in |
zip | Takes one element from enum and merges corresponding elements from each args. |
Class Attribute Details
.empty? ⇒ Boolean
(readonly)
Returns true when there are no environment variables
# File 'hash.c', line 5398
static VALUE env_empty_p(void) { char **env; env = GET_ENVIRON(environ); if (env[0] == 0) { FREE_ENVIRON(environ); return Qtrue; } FREE_ENVIRON(environ); return Qfalse; }
Class Method Details
.[](name) ⇒ value
Retrieves the value
for environment variable name
as a ::String
. Returns nil
if the named variable does not exist.
# File 'hash.c', line 4560
static VALUE rb_f_getenv(VALUE obj, VALUE name) { const char *nam, *env; nam = env_name(name); env = getenv(nam); if (env) { return env_name_new(nam, env); } return Qnil; }
.[]=(name, value)
.store(name, value) ⇒ value
Also known as: .store
value
Sets the environment variable name
to value
. If the value given is nil
the environment variable is deleted. name
must be a string.
# File 'hash.c', line 4878
static VALUE env_aset(VALUE obj, VALUE nm, VALUE val) { char *name, *value; if (NIL_P(val)) { env_delete(obj, nm); return Qnil; } SafeStringValue(nm); SafeStringValue(val); /* nm can be modified in `val.to_str`, don't get `name` before * check for `val` */ get_env_ptr(name, nm); get_env_ptr(value, val); ruby_setenv(name, value); if (ENVMATCH(name, PATH_ENV)) { RB_GC_GUARD(nm); if (OBJ_TAINTED(val)) { /* already tainted, no check */ path_tainted = 1; return val; } else { path_tainted_p(value); } } else if (ENVMATCH(name, TZ_ENV)) { ruby_tz_uptodate_p = FALSE; } return val; }
.assoc(name) ⇒ Array?
Returns an ::Array
of the name and value of the environment variable with name
or nil
if the name cannot be found.
# File 'hash.c', line 5438
static VALUE env_assoc(VALUE env, VALUE key) { const char *s, *e; s = env_name(key); e = getenv(s); if (e) return rb_assoc_new(key, env_str_new2(e)); return Qnil; }
.clear
Removes every environment variable.
# File 'hash.c', line 5265
VALUE rb_env_clear(void) { VALUE keys; long i; keys = env_keys(); for (i=0; i<RARRAY_LEN(keys); i++) { VALUE val = rb_f_getenv(Qnil, RARRAY_AREF(keys, i)); if (!NIL_P(val)) { env_delete(Qnil, RARRAY_AREF(keys, i)); } } RB_GC_GUARD(keys); return envtbl; }
.delete(name) ⇒ value
.delete(name) {|name| ... } ⇒ value
value
.delete(name) {|name| ... } ⇒ value
Deletes the environment variable with name
and returns the value of the variable. If a block is given it will be called when the named environment does not exist.
# File 'hash.c', line 4543
static VALUE env_delete_m(VALUE obj, VALUE name) { VALUE val; val = env_delete(obj, name); if (NIL_P(val) && rb_block_given_p()) rb_yield(name); return val; }
.delete_if {|name, value| ... } ⇒ Hash
.delete_if ⇒ Enumerator
Deletes every environment variable for which the block evaluates to true
.
If no block is given an enumerator is returned instead.
# File 'hash.c', line 5112
static VALUE env_delete_if(VALUE ehash) { RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size); env_reject_bang(ehash); return envtbl; }
.each {|name, value| ... } ⇒ Hash
.each ⇒ Enumerator
.each_pair {|name, value| ... } ⇒ Hash
.each_pair ⇒ Enumerator
Also known as: .each_pair
Yields each environment variable name
and value
.
If no block is given an ::Enumerator
is returned.
# File 'hash.c', line 5035
static VALUE env_each_pair(VALUE ehash) { char **env; VALUE ary; long i; RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size); ary = rb_ary_new(); 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_arity() > 1) { 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; }
.each_key {|name| ... } ⇒ Hash
.each_key ⇒ Enumerator
Yields each environment variable name.
An Enumerator is returned if no block is given.
# File 'hash.c', line 4962
static VALUE env_each_key(VALUE ehash) { VALUE keys; long i; RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size); keys = env_keys(); for (i=0; i<RARRAY_LEN(keys); i++) { rb_yield(RARRAY_AREF(keys, i)); } return ehash; }
.each {|name, value| ... } ⇒ Hash
.each ⇒ Enumerator
.each_pair {|name, value| ... } ⇒ Hash
.each_pair ⇒ Enumerator
Alias for .each.
.each_value {|value| ... } ⇒ Hash
.each_value ⇒ Enumerator
Yields each environment variable value
.
An Enumerator is returned if no block was given.
# File 'hash.c', line 5010
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; }
.fetch(name) ⇒ value
.fetch(name, default) ⇒ value
.fetch(name) {|missing_name| ... } ⇒ value
value
.fetch(name, default) ⇒ value
.fetch(name) {|missing_name| ... } ⇒ value
Retrieves the environment variable name
.
If the given name does not exist and neither default
nor a block a provided an ::KeyError
is raised. If a block is given it is called with the missing name to provide a value. If a default value is given it will be returned when no block is given.
# File 'hash.c', line 4587
static VALUE env_fetch(int argc, VALUE *argv) { VALUE key; long block_given; const char *nam, *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(nam); if (!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_name_new(nam, env); }
.select {|name, value| ... } ⇒ Hash
.select ⇒ Enumerator
.filter {|name, value| ... } ⇒ Hash
.filter ⇒ Enumerator
Also known as: .select
Returns a copy of the environment for entries where the block returns true.
Returns an ::Enumerator
if no block was given.
filter
is an alias for .select.
# File 'hash.c', line 5153
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(); 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; }
.select! {|name, value| ... } ⇒ ENV
?
.select! ⇒ Enumerator
.filter! {|name, value| ... } ⇒ ENV
?
.filter! ⇒ Enumerator
Also known as: .select!
[ GitHub ]
ENV
?
.select! ⇒ Enumerator
.filter! {|name, value| ... } ⇒ ENV
?
.filter! ⇒ Enumerator
# File 'hash.c', line 5188
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(); 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))) { FL_UNSET(RARRAY_AREF(keys, i), FL_TAINT); env_delete(Qnil, RARRAY_AREF(keys, i)); del++; } } } RB_GC_GUARD(keys); if (del == 0) return Qnil; return envtbl; }
.key?(name) ⇒ Boolean
.include?(name) ⇒ Boolean
.has_key?(name) ⇒ Boolean
.member?(name) ⇒ Boolean
Boolean
.include?(name) ⇒ Boolean
.has_key?(name) ⇒ Boolean
.member?(name) ⇒ Boolean
Alias for .key?.
.value?(value) ⇒ Boolean
.has_value?(value) ⇒ Boolean
Boolean
.has_value?(value) ⇒ Boolean
Alias for .value?.
.key?(name) ⇒ Boolean
.include?(name) ⇒ Boolean
.has_key?(name) ⇒ Boolean
.member?(name) ⇒ Boolean
Boolean
.include?(name) ⇒ Boolean
.has_key?(name) ⇒ Boolean
.member?(name) ⇒ Boolean
Alias for .key?.
.index(value) ⇒ key
Deprecated method that is equivalent to .key
# File 'hash.c', line 5549
static VALUE env_index(VALUE dmy, VALUE value) { rb_warn("ENV.index is deprecated; use ENV.key"); return env_key(dmy, value); }
.inspect ⇒ String
Returns the contents of the environment as a ::String
.
# File 'hash.c', line 5300
static VALUE env_inspect(void) { char **env; VALUE str, i; str = rb_str_buf_new2("{"); env = GET_ENVIRON(environ); while (*env) { char *s = strchr(*env, '='); if (env != environ) { rb_str_buf_cat2(str, ", "); } if (s) { rb_str_buf_cat2(str, "\""); rb_str_buf_cat(str, *env, s-*env); rb_str_buf_cat2(str, "\"=>"); i = rb_inspect(rb_str_new2(s+1)); rb_str_buf_append(str, i); } env++; } FREE_ENVIRON(environ); rb_str_buf_cat2(str, "}"); OBJ_TAINT(str); return str; }
.invert ⇒ Hash
Returns a new hash created by using environment variable names as values and values as names.
# File 'hash.c', line 5649
static VALUE env_invert(void) { return rb_hash_invert(env_to_hash()); }
.keep_if {|name, value| ... } ⇒ Hash
.keep_if ⇒ Enumerator
Deletes every environment variable where the block evaluates to false
.
Returns an enumerator if no block was given.
# File 'hash.c', line 5222
static VALUE env_keep_if(VALUE ehash) { RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size); env_select_bang(ehash); return envtbl; }
.key(value) ⇒ name
Returns the name of the environment variable with value
. If the value is not found nil
is returned.
# File 'hash.c', line 5519
static VALUE env_key(VALUE dmy, VALUE value) { char **env; VALUE str; SafeStringValue(value); 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); FREE_ENVIRON(environ); return str; } } env++; } FREE_ENVIRON(environ); return Qnil; }
.key?(name) ⇒ Boolean
.include?(name) ⇒ Boolean
.has_key?(name) ⇒ Boolean
.member?(name) ⇒ Boolean
Also known as: .include?, .member?, .has_key?
Boolean
.include?(name) ⇒ Boolean
.has_key?(name) ⇒ Boolean
.member?(name) ⇒ Boolean
Returns true
if there is an environment variable with the given name
.
# File 'hash.c', line 5421
static VALUE env_has_key(VALUE env, VALUE key) { const char *s; s = env_name(key); if (getenv(s)) return Qtrue; return Qfalse; }
.keys ⇒ Array
Returns every environment variable name in an ::Array
# File 'hash.c', line 4918
static VALUE env_keys(void) { char **env; VALUE ary; ary = rb_ary_new(); env = GET_ENVIRON(environ); while (*env) { char *s = strchr(*env, '='); if (s) { rb_ary_push(ary, env_str_new(*env, s-*env)); } env++; } FREE_ENVIRON(environ); return ary; }
.length
.size
Also known as: .size
Returns the number of environment variables.
# File 'hash.c', line 5379
static VALUE env_size(void) { int i; char **env; env = GET_ENVIRON(environ); for (i=0; env[i]; i++) ; FREE_ENVIRON(environ); return INT2FIX(i); }
.key?(name) ⇒ Boolean
.include?(name) ⇒ Boolean
.has_key?(name) ⇒ Boolean
.member?(name) ⇒ Boolean
Boolean
.include?(name) ⇒ Boolean
.has_key?(name) ⇒ Boolean
.member?(name) ⇒ Boolean
Alias for .key?.
.rassoc(value)
Returns an ::Array
of the name and value of the environment variable with value
or nil
if the value cannot be found.
# File 'hash.c', line 5487
static VALUE env_rassoc(VALUE dmy, VALUE obj) { char **env; obj = rb_check_string_type(obj); if (NIL_P(obj)) return Qnil; rb_check_safe_obj(obj); 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) { VALUE result = rb_assoc_new(rb_tainted_str_new(*env, s-*env-1), obj); FREE_ENVIRON(environ); return result; } } env++; } FREE_ENVIRON(environ); return Qnil; }
.rehash
Re-hashing the environment variables does nothing. It is provided for compatibility with ::Hash
.
# File 'hash.c', line 5366
static VALUE env_none(void) { return Qnil; }
.reject {|name, value| ... } ⇒ Hash
.reject ⇒ Enumerator
Same as .delete_if, but works on (and returns) a copy of the environment.
# File 'hash.c', line 5609
static VALUE env_reject(void) { return rb_hash_delete_if(env_to_hash()); }
.reject! {|name, value| ... } ⇒ ENV
?
.reject! ⇒ Enumerator
ENV
?
.reject! ⇒ Enumerator
Equivalent to .delete_if but returns nil
if no changes were made.
Returns an ::Enumerator
if no block was given.
# File 'hash.c', line 5078
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(); 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))) { FL_UNSET(RARRAY_AREF(keys, i), FL_TAINT); env_delete(Qnil, RARRAY_AREF(keys, i)); del++; } } } RB_GC_GUARD(keys); if (del == 0) return Qnil; return envtbl; }
.replace(hash) ⇒ ENV
Replaces the contents of the environment variables with the contents of hash
.
# File 'hash.c', line 5688
static VALUE env_replace(VALUE env, VALUE hash) { VALUE keys; long i; keys = env_keys(); 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(env, RARRAY_AREF(keys, i)); } RB_GC_GUARD(keys); return env; }
.select {|name, value| ... } ⇒ Hash
.select ⇒ Enumerator
.filter {|name, value| ... } ⇒ Hash
.filter ⇒ Enumerator
Alias for .filter.
.select! {|name, value| ... } ⇒ ENV
?
.select! ⇒ Enumerator
.filter! {|name, value| ... } ⇒ ENV
?
.filter! ⇒ Enumerator
ENV
?
.select! ⇒ Enumerator
.filter! {|name, value| ... } ⇒ ENV
?
.filter! ⇒ Enumerator
Alias for .filter!.
.shift ⇒ Array?
Removes an environment variable name-value pair from ENV
and returns it as an ::Array
. Returns nil
if when the environment is empty.
# File 'hash.c', line 5622
static VALUE env_shift(void) { char **env; VALUE result = Qnil; env = GET_ENVIRON(environ); if (*env) { char *s = strchr(*env, '='); if (s) { VALUE key = env_str_new(*env, s-*env); VALUE val = env_str_new2(getenv(RSTRING_PTR(key))); env_delete(Qnil, key); result = rb_assoc_new(key, val); } } FREE_ENVIRON(environ); return result; }
.length
.size
Alias for .length.
.slice(*keys) ⇒ Hash
Returns a hash containing only the given keys from ENV
and their values.
ENV.slice("TERM","HOME") #=> {"TERM"=>"xterm-256color", "HOME"=>"/Users/rhc"}
# File 'hash.c', line 5238
static VALUE env_slice(int argc, VALUE *argv) { 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; }
.[]=(name, value)
.store(name, value) ⇒ value
value
Alias for .[]=.
.to_a ⇒ Array
Converts the environment variables into an array of names and value arrays.
ENV.to_a # => [["TERM", "xterm-color"], ["SHELL", "/bin/bash"], ...]
# File 'hash.c', line 5339
static VALUE env_to_a(void) { char **env; VALUE ary; ary = rb_ary_new(); 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; }
Creates a hash with a copy of the environment variables.
# File 'hash.c', line 5591
static VALUE env_to_h(void) { VALUE hash = env_to_hash(); if (rb_block_given_p()) { hash = rb_hash_to_h_block(hash); } return hash; }
.to_hash ⇒ Hash
Creates a hash with a copy of the environment variables.
# File 'hash.c', line 5563
static VALUE env_to_hash(void) { char **env; VALUE hash; hash = rb_hash_new(); env = GET_ENVIRON(environ); while (*env) { char *s = strchr(*env, '='); if (s) { rb_hash_aset(hash, env_str_new(*env, s-*env), env_str_new2(s+1)); } env++; } FREE_ENVIRON(environ); return hash; }
.to_s ⇒ ENV
Returns “ENV”
# File 'hash.c', line 5288
static VALUE env_to_s(void) { return rb_usascii_str_new2("ENV"); }
Adds the contents of hash
to the environment variables. If no block is specified entries with duplicate keys are overwritten, otherwise the value of each duplicate name is determined by calling the block with the key, its value from the environment and its value from the hash.
# File 'hash.c', line 5726
static VALUE env_update(VALUE env, VALUE hash) { if (env == hash) return env; hash = to_hash(hash); rb_hash_foreach(hash, env_update_i, 0); return env; }
.value?(value) ⇒ Boolean
.has_value?(value) ⇒ Boolean
Also known as: .has_value?
Boolean
.has_value?(value) ⇒ Boolean
Returns true
if there is an environment variable with the given value
.
# File 'hash.c', line 5456
static VALUE env_has_value(VALUE dmy, VALUE obj) { char **env; obj = rb_check_string_type(obj); if (NIL_P(obj)) return Qnil; rb_check_safe_obj(obj); 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) { FREE_ENVIRON(environ); return Qtrue; } } env++; } FREE_ENVIRON(environ); return Qfalse; }
.values ⇒ Array
Returns every environment variable value as an ::Array
# File 'hash.c', line 4982
static VALUE env_values(void) { VALUE ary; char **env; ary = rb_ary_new(); env = GET_ENVIRON(environ); while (*env) { char *s = strchr(*env, '='); if (s) { rb_ary_push(ary, env_str_new2(s+1)); } env++; } FREE_ENVIRON(environ); return ary; }
.values_at(name, ...) ⇒ Array
Returns an array containing the environment variable values associated with the given names. See also .select.
# File 'hash.c', line 5127
static VALUE env_values_at(int argc, VALUE *argv) { 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; }