End of Life Ruby 3.2 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
class method
ENV.slice
ENV.slice(*names) → hash of name/value pairs
hash.c
View on GitHub ↗
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:
Example
ENV.replace('foo' => '0', 'bar' => '1', 'baz' => '2', 'bat' => '3') ENV.slice('foo', 'baz') # => {"foo"=>"0", "baz"=>"2"} ENV.slice('baz', 'foo') # => {"baz"=>"2", "foo"=>"0"}
Raises an exception if any of the names is invalid (see Invalid Names and Values):
Example
ENV.slice('foo', 'bar', :bat) # Raises TypeError (no implicit conversion of Symbol into String)