End of Life Ruby 3.2 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
class method
ENV.store
ENV[name] = value → value
ENV.store(name, value) → value
hash.c
View on GitHub ↗
static VALUE
env_aset_m(VALUE obj, VALUE nm, VALUE val)
{
return env_aset(nm, val);
}
ENV.store is an alias for ENV.[]=.
Creates, updates, or deletes the named environment variable, returning the value. Both name and value may be instances of String. See Valid Names and Values.
-
If the named environment variable does not exist:
-
If
valueisnil, does nothing.ExampleENV.clear ENV['foo'] = nil # => nil ENV.include?('foo') # => false ENV.store('bar', nil) # => nil ENV.include?('bar') # => false
-
If
valueis notnil, creates the environment variable withnameandvalue:Example# Create 'foo' using ENV.[]=. ENV['foo'] = '0' # => '0' ENV['foo'] # => '0' # Create 'bar' using ENV.store. ENV.store('bar', '1') # => '1' ENV['bar'] # => '1'
-
-
If the named environment variable exists:
-
If
valueis notnil, updates the environment variable with valuevalue:Example# Update 'foo' using ENV.[]=. ENV['foo'] = '2' # => '2' ENV['foo'] # => '2' # Update 'bar' using ENV.store. ENV.store('bar', '3') # => '3' ENV['bar'] # => '3'
-
If
valueisnil, deletes the environment variable:Example# Delete 'foo' using ENV.[]=. ENV['foo'] = nil # => nil ENV.include?('foo') # => false # Delete 'bar' using ENV.store. ENV.store('bar', nil) # => nil ENV.include?('bar') # => false
-
Raises an exception if name or value is invalid. See Invalid Names and Values.