Maintenance Ruby 3.4 gets fixes, but no new features. Switch to Ruby 4.0 →
class method
ENV.delete
ENV.delete(name) → value
ENV.delete(name) { |name| block } → value
ENV.delete(missing_name) → nil
ENV.delete(missing_name) { |name| block } → block_value
hash.c
View on GitHub ↗
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:
Example
ENV['foo'] = '0' ENV.delete('foo') # => '0'
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:
Example
ENV.delete('foo') { |name| name * 2 } # => "foofoo"
If a block given and the environment variable exists, deletes the environment variable and returns its value (ignoring the block):
Example
ENV['foo'] = '0' ENV.delete('foo') { |name| raise 'ignored' } # => "0"
Raises an exception if name is invalid. See Invalid Names and Values.