Skip to content

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

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.

See also .clone .delete_if .clear .dup also in 4.1 · 4.0 · 3.3 · 3.2 · 3.1 · 3.0