A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://docs.ruby-lang.org/en/master/ENV.html below:

class ENV - Documentation for Ruby 3.5

class ENV

ENV is a hash-like accessor for environment variables.

Interaction with the Operating System

The ENV object interacts with the operating system’s environment variables:

Names and Values

Generally, a name or value is a String.

Valid Names and Values

Each name or value must be one of the following:

Invalid Names and Values

A new name:

A new name or value:

About Ordering

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:

About the Examples

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:

What’s Here

First, what’s elsewhere. Class ENV:

Here, class ENV provides methods that are useful for:

Methods for Querying Methods for Assigning Methods for Deleting Methods for Iterating Methods for Converting More Methods Public Class Methods

Source

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.

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.

Raises an exception if name or value is invalid. See Invalid Names and Values.

Source

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.size 

Source

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.

Source

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.

Source

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.

Source

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 } 
h 

Source

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) } 
names 

Source

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 } 
h 

Source

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) } 
values 

Source

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.

Source

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.freeze 

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_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.inspect 

Source

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.

Source

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') } 
ENV 

Source

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) 

See Invalid Names and Values.

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.

Source

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.size 

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_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.

Source

static 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.

Source

static VALUE
env_none(VALUE _)
{
    return Qnil;
}

(Provided for compatibility with Hash.)

Does not modify ENV; returns nil.

Source

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_hash 

Source

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.

Source

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.size 

Source

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.

Raises an exception if name or value is invalid. See Invalid Names and Values.

Source

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_a 

Source

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_hash 

Source

static VALUE
env_to_s(VALUE _)
{
    return rb_usascii_str_new2("ENV");
}

Returns String ‘ENV’:

ENV.to_s 

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.

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_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.

Source

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