Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
class method
ENV.keep_if
ENV.keep_if {|name, value| ... } → ENV
ENV.keep_if → an_enumerator
hash.c
View on GitHub ↗
static VALUE
env_keep_if(VALUE ehash)
{
RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size);
env_select_bang(ehash);
return envtbl;
}
Calls the block with each environment variable name and value, deleting each environment variable for which the block returns false or nil, and returning ENV:
Example
ENV.replace('foo' => '0', 'bar' => '1', 'baz' => '2') ENV.keep_if { |name, value| name.start_with?('b') } # => ENV ENV # => {"bar"=>"1", "baz"=>"2"}
With no block given, returns a new Enumerator.