End of Life Ruby 3.2 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
class method
ENV.to_h
ENV.to_h → hash of name/value pairs
ENV.to_h {|name, value| block } → hash of name/value pairs
hash.c
View on GitHub ↗
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:
Example
ENV.replace('foo' => '0', 'bar' => '1') ENV.to_h # => {"bar"=>"1", "foo"=>"0"}
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:
Example
ENV.to_h { |name, value| [name.to_sym, value.to_i] } # => {:bar=>1, :foo=>0}
Raises an exception if the block does not return an Array:
Example
ENV.to_h { |name, value| name } # Raises TypeError (wrong element type String (expected array))
Raises an exception if the block returns an Array of the wrong size:
Example
ENV.to_h { |name, value| [name] } # Raises ArgumentError (element has wrong array length (expected 2, was 1))