ENV is a hash-like accessor for environment variables.

Methods
#
A
C
D
E
F
H
I
K
L
M
P
R
S
T
U
V
Class Public methods
ENV[name] → value

Retrieves the value for environment variable name as a String. Returns nil if the named variable does not exist.

static VALUE
rb_f_getenv(VALUE obj, VALUE name)
{
    char *nam, *env;

    rb_secure(4);
    SafeStringValue(name);
    nam = RSTRING_PTR(name);
    if (memchr(nam, '\0', RSTRING_LEN(name))) {
        rb_raise(rb_eArgError, "bad environment variable name");
    }
    env = getenv(nam);
    if (env) {
        if (ENVMATCH(nam, PATH_ENV) && !env_path_tainted(env)) {
#ifdef _WIN32
            VALUE str = rb_str_conv_enc(rb_str_new(env, strlen(env)), rb_utf8_encoding(), rb_filesystem_encoding());
#else
            VALUE str = rb_filesystem_str_new_cstr(env);
#endif

            rb_obj_freeze(str);
            return str;
        }
        return env_str_new2(env);
    }
    return Qnil;
}
ENV[name] = value

Sets the environment variable name to value. If the value given is nil the environment variable is deleted.

static VALUE
env_aset(VALUE obj, VALUE nm, VALUE val)
{
    char *name, *value;

    if (rb_safe_level() >= 4) {
        rb_raise(rb_eSecurityError, "can't change environment variable");
    }

    if (NIL_P(val)) {
        env_delete(obj, nm);
        return Qnil;
    }
    StringValue(nm);
    StringValue(val);
    name = RSTRING_PTR(nm);
    value = RSTRING_PTR(val);
    if (memchr(name, '\0', RSTRING_LEN(nm)))
        rb_raise(rb_eArgError, "bad environment variable name");
    if (memchr(value, '\0', RSTRING_LEN(val)))
        rb_raise(rb_eArgError, "bad environment variable value");

    ruby_setenv(name, value);
    if (ENVMATCH(name, PATH_ENV)) {
        if (OBJ_TAINTED(val)) {
            /* already tainted, no check */
            path_tainted = 1;
            return val;
        }
        else {
            path_tainted_p(value);
        }
    }
    return val;
}
ENV.assoc(name) → Array or nil

Returns an Array of the name and value of the environment variable with name or nil if the name cannot be found.

static VALUE
env_assoc(VALUE env, VALUE key)
{
    char *s, *e;

    rb_secure(4);
    s = StringValuePtr(key);
    if (memchr(s, '\0', RSTRING_LEN(key)))
        rb_raise(rb_eArgError, "bad environment variable name");
    e = getenv(s);
    if (e) return rb_assoc_new(key, rb_tainted_str_new2(e));
    return Qnil;
}
ENV.clear

Removes every environment variable.

VALUE
rb_env_clear(void)
{
    volatile VALUE keys;
    long i;

    keys = env_keys();  /* rb_secure(4); */
    for (i=0; i<RARRAY_LEN(keys); i++) {
        VALUE val = rb_f_getenv(Qnil, RARRAY_PTR(keys)[i]);
        if (!NIL_P(val)) {
            env_delete(Qnil, RARRAY_PTR(keys)[i]);
        }
    }
    return envtbl;
}
ENV.delete(name) → value ENV.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.

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;
}
ENV.delete_if { |name, value| } → Hash ENV.delete_if → Enumerator

Deletes every environment variable for which the block evaluates to true.

If no block is given an enumerator is returned instead.

static VALUE
env_delete_if(VALUE ehash)
{
    RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size);
    env_reject_bang(ehash);
    return envtbl;
}
ENV.each { |name, value| } → Hash ENV.each → Enumerator ENV.each_pair { |name, value| } → Hash ENV.each_pair → Enumerator

Yields each environment variable name and value.

If no block is given an Enumerator is returned.

static VALUE
env_each_pair(VALUE ehash)
{
    char **env;
    VALUE ary;
    long i;

    RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size);

    rb_secure(4);
    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);

    for (i=0; i<RARRAY_LEN(ary); i+=2) {
        rb_yield(rb_assoc_new(RARRAY_PTR(ary)[i], RARRAY_PTR(ary)[i+1]));
    }
    return ehash;
}
ENV.each_key { |name| } → Hash ENV.each_key → Enumerator

Yields each environment variable name.

An Enumerator is returned if no block is given.

static VALUE
env_each_key(VALUE ehash)
{
    VALUE keys;
    long i;

    RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size);
    keys = env_keys();  /* rb_secure(4); */
    for (i=0; i<RARRAY_LEN(keys); i++) {
        rb_yield(RARRAY_PTR(keys)[i]);
    }
    return ehash;
}
ENV.each_pair { |name, value| } → Hash ENV.each_pair → Enumerator

Yields each environment variable name and value.

If no block is given an Enumerator is returned.

static VALUE
env_each_pair(VALUE ehash)
{
    char **env;
    VALUE ary;
    long i;

    RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size);

    rb_secure(4);
    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);

    for (i=0; i<RARRAY_LEN(ary); i+=2) {
        rb_yield(rb_assoc_new(RARRAY_PTR(ary)[i], RARRAY_PTR(ary)[i+1]));
    }
    return ehash;
}
ENV.each_value { |value| } → Hash ENV.each_value → Enumerator

Yields each environment variable value.

An Enumerator is returned if no block was given.

static VALUE
env_each_value(VALUE ehash)
{
    VALUE values;
    long i;

    RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size);
    values = env_values();      /* rb_secure(4); */
    for (i=0; i<RARRAY_LEN(values); i++) {
        rb_yield(RARRAY_PTR(values)[i]);
    }
    return ehash;
}
ENV.empty? → true or false

Returns true when there are no environment variables

static VALUE
env_empty_p(void)
{
    char **env;

    rb_secure(4);
    env = GET_ENVIRON(environ);
    if (env[0] == 0) {
        FREE_ENVIRON(environ);
        return Qtrue;
    }
    FREE_ENVIRON(environ);
    return Qfalse;
}
ENV.fetch(name) → value ENV.fetch(name, default) → value ENV.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 IndexError 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.

static VALUE
env_fetch(int argc, VALUE *argv)
{
    VALUE key, if_none;
    long block_given;
    char *nam, *env;

    rb_secure(4);
    rb_scan_args(argc, argv, "11", &key, &if_none);
    block_given = rb_block_given_p();
    if (block_given && argc == 2) {
        rb_warn("block supersedes default value argument");
    }
    SafeStringValue(key);
    nam = RSTRING_PTR(key);
    if (memchr(nam, '\0', RSTRING_LEN(key))) {
        rb_raise(rb_eArgError, "bad environment variable name");
    }
    env = getenv(nam);
    if (!env) {
        if (block_given) return rb_yield(key);
        if (argc == 1) {
            rb_raise(rb_eKeyError, "key not found");
        }
        return if_none;
    }
    if (ENVMATCH(nam, PATH_ENV) && !env_path_tainted(env))
#ifdef _WIN32
        return rb_str_conv_enc(rb_str_new(env, strlen(env)), rb_utf8_encoding(), rb_filesystem_encoding());
#else
        return rb_filesystem_str_new_cstr(env);
#endif
    return env_str_new2(env);
}
ENV.has_key?(name) → true or false

Returns true if there is an environment variable with the given name.

static VALUE
env_has_key(VALUE env, VALUE key)
{
    char *s;

    rb_secure(4);
    s = StringValuePtr(key);
    if (memchr(s, '\0', RSTRING_LEN(key)))
        rb_raise(rb_eArgError, "bad environment variable name");
    if (getenv(s)) return Qtrue;
    return Qfalse;
}
ENV.has_value?(value) → true or false

Returns true if there is an environment variable with the given value.

static VALUE
env_has_value(VALUE dmy, VALUE obj)
{
    char **env;

    rb_secure(4);
    obj = rb_check_string_type(obj);
    if (NIL_P(obj)) return Qnil;
    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;
}
ENV.include?(name) → true or false

Returns true if there is an environment variable with the given name.

static VALUE
env_has_key(VALUE env, VALUE key)
{
    char *s;

    rb_secure(4);
    s = StringValuePtr(key);
    if (memchr(s, '\0', RSTRING_LEN(key)))
        rb_raise(rb_eArgError, "bad environment variable name");
    if (getenv(s)) return Qtrue;
    return Qfalse;
}
ENV.index(value) → key

Deprecated method that is equivalent to ::key

static VALUE
env_index(VALUE dmy, VALUE value)
{
    rb_warn("ENV.index is deprecated; use ENV.key");
    return env_key(dmy, value);
}
ENV.inspect → string

Returns the contents of the environment as a String.

static VALUE
env_inspect(void)
{
    char **env;
    VALUE str, i;

    rb_secure(4);
    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;
}
ENV.invert → Hash

Returns a new hash created by using environment variable names as values and values as names.

static VALUE
env_invert(void)
{
    return rb_hash_invert(env_to_hash());
}
ENV.keep_if { |name, value| } → Hash ENV.keep_if → Enumerator

Deletes every environment variable where the block evaluates to false.

Returns an enumerator if no block was given.

static VALUE
env_keep_if(VALUE ehash)
{
    RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size);
    env_select_bang(ehash);
    return envtbl;
}
ENV.key(value) → name

Returns the name of the environment variable with value. If the value is not found nil is returned.

static VALUE
env_key(VALUE dmy, VALUE value)
{
    char **env;
    VALUE str;

    rb_secure(4);
    StringValue(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;
}
ENV.key?(name) → true or false

Returns true if there is an environment variable with the given name.

static VALUE
env_has_key(VALUE env, VALUE key)
{
    char *s;

    rb_secure(4);
    s = StringValuePtr(key);
    if (memchr(s, '\0', RSTRING_LEN(key)))
        rb_raise(rb_eArgError, "bad environment variable name");
    if (getenv(s)) return Qtrue;
    return Qfalse;
}
ENV.keys → Array

Returns every environment variable name in an Array

static VALUE
env_keys(void)
{
    char **env;
    VALUE ary;

    rb_secure(4);
    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;
}
ENV.length

Returns the number of environment variables.

static VALUE
env_size(void)
{
    int i;
    char **env;

    rb_secure(4);
    env = GET_ENVIRON(environ);
    for (i=0; env[i]; i++)
        ;
    FREE_ENVIRON(environ);
    return INT2FIX(i);
}
ENV.member?(name) → true or false

Returns true if there is an environment variable with the given name.

static VALUE
env_has_key(VALUE env, VALUE key)
{
    char *s;

    rb_secure(4);
    s = StringValuePtr(key);
    if (memchr(s, '\0', RSTRING_LEN(key)))
        rb_raise(rb_eArgError, "bad environment variable name");
    if (getenv(s)) return Qtrue;
    return Qfalse;
}
ENV.rassoc(value)

Returns an Array of the name and value of the environment variable with value or nil if the value cannot be found.

static VALUE
env_rassoc(VALUE dmy, VALUE obj)
{
    char **env;

    rb_secure(4);
    obj = rb_check_string_type(obj);
    if (NIL_P(obj)) return Qnil;
    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;
}
ENV.rehash

Re-hashing the environment variables does nothing. It is provided for compatibility with Hash.

static VALUE
env_none(void)
{
    return Qnil;
}
ENV.reject { |name, value| } → Hash ENV.reject → Enumerator

Same as ENV#delete_if, but works on (and returns) a copy of the environment.

static VALUE
env_reject(void)
{
    return rb_hash_delete_if(env_to_hash());
}
ENV.reject! { |name, value| } → ENV or nil ENV.reject! → Enumerator

Equivalent to ENV#delete_if but returns nil if no changes were made.

Returns an Enumerator if no block was given.

static VALUE
env_reject_bang(VALUE ehash)
{
    volatile VALUE keys;
    long i;
    int del = 0;

    RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size);
    keys = env_keys();  /* rb_secure(4); */
    RBASIC(keys)->klass = 0;
    for (i=0; i<RARRAY_LEN(keys); i++) {
        VALUE val = rb_f_getenv(Qnil, RARRAY_PTR(keys)[i]);
        if (!NIL_P(val)) {
            if (RTEST(rb_yield_values(2, RARRAY_PTR(keys)[i], val))) {
                FL_UNSET(RARRAY_PTR(keys)[i], FL_TAINT);
                env_delete(Qnil, RARRAY_PTR(keys)[i]);
                del++;
            }
        }
    }
    if (del == 0) return Qnil;
    return envtbl;
}
ENV.replace(hash) → env

Replaces the contents of the environment variables with the contents of hash.

static VALUE
env_replace(VALUE env, VALUE hash)
{
    volatile VALUE keys;
    long i;

    keys = env_keys();  /* rb_secure(4); */
    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_PTR(keys)[i]);
    }
    return env;
}
ENV.select { |name, value| } → Hash ENV.select → Enumerator

Returns a copy of the environment for entries where the block returns true.

Returns an Enumerator if no block was given.

static VALUE
env_select(VALUE ehash)
{
    VALUE result;
    char **env;

    RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size);
    rb_secure(4);
    result = rb_hash_new();
    env = GET_ENVIRON(environ);
    while (*env) {
        char *s = strchr(*env, '=');
        if (s) {
            VALUE k = env_str_new(*env, s-*env);
            VALUE v = env_str_new2(s+1);
            if (RTEST(rb_yield_values(2, k, v))) {
                rb_hash_aset(result, k, v);
            }
        }
        env++;
    }
    FREE_ENVIRON(environ);

    return result;
}
ENV.select! { |name, value| } → ENV or nil ENV.select! → Enumerator

Equivalent to ENV#keep_if but returns nil if no changes were made.

static VALUE
env_select_bang(VALUE ehash)
{
    volatile VALUE keys;
    long i;
    int del = 0;

    RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size);
    keys = env_keys();  /* rb_secure(4); */
    RBASIC(keys)->klass = 0;
    for (i=0; i<RARRAY_LEN(keys); i++) {
        VALUE val = rb_f_getenv(Qnil, RARRAY_PTR(keys)[i]);
        if (!NIL_P(val)) {
            if (!RTEST(rb_yield_values(2, RARRAY_PTR(keys)[i], val))) {
                FL_UNSET(RARRAY_PTR(keys)[i], FL_TAINT);
                env_delete(Qnil, RARRAY_PTR(keys)[i]);
                del++;
            }
        }
    }
    if (del == 0) return Qnil;
    return envtbl;
}
ENV.shift → Array or nil

Removes an environment variable name-value pair from ENV and returns it as an Array. Returns nil if when the environment is empty.

static VALUE
env_shift(void)
{
    char **env;

    rb_secure(4);
    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);
            return rb_assoc_new(key, val);
        }
    }
    FREE_ENVIRON(environ);
    return Qnil;
}
ENV.size

Returns the number of environment variables.

static VALUE
env_size(void)
{
    int i;
    char **env;

    rb_secure(4);
    env = GET_ENVIRON(environ);
    for (i=0; env[i]; i++)
        ;
    FREE_ENVIRON(environ);
    return INT2FIX(i);
}
ENV.store(name, value) → value

Sets the environment variable name to value. If the value given is nil the environment variable is deleted.

static VALUE
env_aset(VALUE obj, VALUE nm, VALUE val)
{
    char *name, *value;

    if (rb_safe_level() >= 4) {
        rb_raise(rb_eSecurityError, "can't change environment variable");
    }

    if (NIL_P(val)) {
        env_delete(obj, nm);
        return Qnil;
    }
    StringValue(nm);
    StringValue(val);
    name = RSTRING_PTR(nm);
    value = RSTRING_PTR(val);
    if (memchr(name, '\0', RSTRING_LEN(nm)))
        rb_raise(rb_eArgError, "bad environment variable name");
    if (memchr(value, '\0', RSTRING_LEN(val)))
        rb_raise(rb_eArgError, "bad environment variable value");

    ruby_setenv(name, value);
    if (ENVMATCH(name, PATH_ENV)) {
        if (OBJ_TAINTED(val)) {
            /* already tainted, no check */
            path_tainted = 1;
            return val;
        }
        else {
            path_tainted_p(value);
        }
    }
    return val;
}
ENV.to_a → Array

Converts the environment variables into an array of names and value arrays.

ENV.to_a # => [["TERM", "xterm-color"], ["SHELL", "/bin/bash"], ...]
static VALUE
env_to_a(void)
{
    char **env;
    VALUE ary;

    rb_secure(4);
    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;
}
ENV.to_hash → hash ENV.to_h → hash

Creates a hash with a copy of the environment variables.

static VALUE
env_to_hash(void)
{
    char **env;
    VALUE hash;

    rb_secure(4);
    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;
}
ENV.to_hash → hash

Creates a hash with a copy of the environment variables.

static VALUE
env_to_hash(void)
{
    char **env;
    VALUE hash;

    rb_secure(4);
    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;
}
ENV.to_s → "ENV"

Returns “ENV”

static VALUE
env_to_s(void)
{
    return rb_usascii_str_new2("ENV");
}
ENV.update(hash) → Hash ENV.update(hash) { |name, old_value, new_value| } → Hash

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.

static VALUE
env_update(VALUE env, VALUE hash)
{
    rb_secure(4);
    if (env == hash) return env;
    hash = to_hash(hash);
    rb_hash_foreach(hash, env_update_i, 0);
    return env;
}
ENV.value?(value) → true or false

Returns true if there is an environment variable with the given value.

static VALUE
env_has_value(VALUE dmy, VALUE obj)
{
    char **env;

    rb_secure(4);
    obj = rb_check_string_type(obj);
    if (NIL_P(obj)) return Qnil;
    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;
}
ENV.values → Array

Returns every environment variable value as an Array

static VALUE
env_values(void)
{
    VALUE ary;
    char **env;

    rb_secure(4);
    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;
}
ENV.values_at(name, ...) → Array

Returns an array containing the environment variable values associated with the given names. See also ::select.

static VALUE
env_values_at(int argc, VALUE *argv)
{
    VALUE result;
    long i;

    rb_secure(4);
    result = rb_ary_new();
    for (i=0; i<argc; i++) {
        rb_ary_push(result, rb_f_getenv(Qnil, argv[i]));
    }
    return result;
}
Instance Public methods
pretty_print(q)
# File lib/pp.rb, line 352
def pretty_print(q)
  h = {}
  ENV.keys.sort.each {|k|
    h[k] = ENV[k]
  }
  q.pp_hash h
end