class RBS::Unnamed::ENVClass
ENV is a hash-like accessor for environment variables.
Interaction with the Operating System
The ENV object interacts with the operating systemβs environment variables:
-
When you get the value for a name in
ENV, the value is retrieved from among the current environment variables. -
When you create or set a name-value pair in
ENV, the name and value are immediately set in the environment variables. -
When you delete a name-value pair in
ENV, it is immediately deleted from the environment variables.
Names and Values
Generally, a name or value is a String.
Valid Names and Values
Each name or value must be one of the following:
-
A
String. -
An object that responds to #to_str by returning a
String, in which case thatStringwill be used as the name or value.
Invalid Names and Values
A new name:
-
May not be the empty string: ENV = '0' # Raises
Errno::EINVAL(Invalid argument - ruby_setenv()) -
May not contain character
"=": ENV = '0' # RaisesErrno::EINVAL(Invalid argument - ruby_setenv(=))
A new name or value:
-
May not be a non-String that does not respond to #to_str:
ENV['foo'] = Object.new # Raises TypeError (no implicit conversion of Object into String) ENV[Object.new] = '0' # Raises TypeError (no implicit conversion of Object into String)
-
May not contain the NUL character
"\0":ENV['foo'] = "\0" # Raises ArgumentError (bad environment variable value: contains null byte) ENV["\0"] == '0' # Raises ArgumentError (bad environment variable name: contains null byte)
-
May not have an ASCII-incompatible encoding such as UTF-16LE or ISO-2022-JP:
ENV['foo'] = '0'.force_encoding(Encoding::ISO_2022_JP) # Raises ArgumentError (bad environment variable name: ASCII incompatible encoding: ISO-2022-JP) ENV["foo".force_encoding(Encoding::ISO_2022_JP)] = '0' # Raises ArgumentError (bad environment variable name: ASCII incompatible encoding: ISO-2022-JP)
About Ordering
ENV enumerates its name/value pairs in the order found in the operating systemβs environment variables. Therefore the ordering of ENV content is OS-dependent, and may be indeterminate.
This will be seen in: * A Hash returned by an ENV method. * An Enumerator returned by an ENV method. * An Array returned by ENV.keys, ENV.values, or ENV.to_a. * The String returned by ENV.inspect. * The Array returned by ENV.shift. * The name returned by ENV.key.
About the Examples
Some methods in ENV return ENV itself. Typically, there are many environment variables. Itβs not useful to display a large ENV in the examples here, so most example snippets begin by resetting the contents of ENV: * ENV.replace replaces ENV with a new collection of entries. * ENV.clear empties ENV.
Whatβs Here
First, whatβs elsewhere. Class ENV:
-
Inherits from class Object.
-
Extends module Enumerable,
Here, class ENV provides methods that are useful for:
-
Querying
-
Assigning
-
Deleting
-
Iterating
-
Converting
-
And more .β¦
Methods for Querying
-
::[]: Returns the value for the given environment variable name if it exists:
-
::empty?: Returns whether
ENVis empty. -
::has_value?, ::value?: Returns whether the given value is in
ENV. -
::include?, ::has_key?, ::key?, ::member?: Returns whether the given name is in
ENV. -
::key: Returns the name of the first entry with the given value.
-
::size, ::length: Returns the number of entries.
-
::value?: Returns whether any entry has the given value.
Methods for Assigning
-
::[]=, ::store: Creates, updates, or deletes the named environment variable.
-
::clear: Removes every environment variable; returns
ENV: -
::update, ::merge!: Adds to
ENVeach key/value pair in the given hash. -
::replace: Replaces the entire content of the
ENVwith the name/value pairs in the given hash.
Methods for Deleting
-
::delete: Deletes the named environment variable name if it exists.
-
::delete_if: Deletes entries selected by the block.
-
::keep_if: Deletes entries not selected by the block.
-
::reject!: Similar to
delete_if, but returnsnilif no change was made. -
::select!, ::filter!: Deletes entries selected by the block.
-
::shift: Removes and returns the first entry.
Methods for Iterating
-
::each, ::each_pair: Calls the block with each name/value pair.
-
::each_key: Calls the block with each name.
-
::each_value: Calls the block with each value.
Methods for Converting
-
::assoc: Returns a 2-element array containing the name and value of the named environment variable if it exists:
-
::clone: Raises an exception.
-
::except: Returns a hash of all name/value pairs except those given.
-
::fetch: Returns the value for the given name.
-
::inspect: Returns the contents of
ENVas a string. -
::invert: Returns a hash whose keys are the
ENVvalues, and whose values are the correspondingENVnames. -
::keys: Returns an array of all names.
-
::rassoc: Returns the name and value of the first found entry that has the given value.
-
::reject: Returns a hash of those entries not rejected by the block.
-
::select, ::filter: Returns a hash of name/value pairs selected by the block. -
::slice: Returns a hash of the given names and their corresponding values.
-
::to_a: Returns the entries as an array of 2-element Arrays.
-
::to_h: Returns a hash of entries selected by the block.
-
::to_hash: Returns a hash of all entries.
-
::to_s: Returns the string
'ENV'. -
::values: Returns all values as an array.
-
::values_at: Returns an array of the values for the given name.
More Methods
-
::dup: Raises an exception.
-
::freeze: Raises an exception.
-
::rehash: Returns
nil, without modifyingENV.
Public Instance Methods
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/rbs/unnamed/env_class.rbs, line 187
def []: (String name) -> String?
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/rbs/unnamed/env_class.rbs, line 273
def []=: (String name, String? value) -> String?
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. ENV.clear ENV = nil # => nil ENV.include?('foo') # => false ENV.store('bar', nil) # => nil ENV.include?('bar') # => false -
If
valueisnil, does nothing. ENV.clear ENV = nil # => nil ENV.include?('foo') # => false ENV.store('bar', nil) # => nil ENV.include?('bar') # => false -
If
valueis notnil, creates the environment variable withnameandvalue: # Create 'foo' using ENV.[]=. ENV = '0' # => '0' ENV # => '0' # Create 'bar' using ENV.store. ENV.store('bar', '1') # => '1' ENV # => '1'
-
-
If the named environment variable exists:
-
If
valueis notnil, updates the environment variable with valuevalue: # Update 'foo' using ENV.[]=. ENV = '2' # => '2' ENV # => '2' # Update 'bar' using ENV.store. ENV.store('bar', '3') # => '3' ENV # => '3' -
If
valueisnil, deletes the environment variable: # Delete 'foo' using ENV.[]=. ENV = 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.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/rbs/unnamed/env_class.rbs, line 1194
def assoc: (String name) -> [ String, String ]?
Returns a 2-element Array containing the name and value of the environment variable for name if it exists: ENV.replace(βfooβ => β0β, βbarβ => β1β) ENV.assoc(βfooβ) # => [βfooβ, β0β]
Returns nil if name is a valid String and there is no such environment variable.
Returns nil if name is the empty String or is a String containing character '='.
Raises an exception if name is a String containing the NUL character "\0": ENV.assoc(β\0β) # Raises ArgumentError (bad environment variable name: contains null byte)
Raises an exception if name has an encoding that is not ASCII-compatible: ENV.assoc(β\xa1\xa1β.force_encoding(Encoding::UTF_16LE)) # Raises ArgumentError (bad environment variable name: ASCII incompatible encoding: UTF-16LE)
Raises an exception if name is not a String: ENV.assoc(Object.new) # TypeError (no implicit conversion of Object into String)
() → self
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/rbs/unnamed/env_class.rbs, line 527
def clear: () -> self
Removes every environment variable; returns ENV: ENV.replace(βfooβ => β0β, βbarβ => β1β) ENV.size # => 2 ENV.clear # => ENV ENV.size # => 0
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/rbs/unnamed/env_class.rbs, line 439
def delete: (String name) -> String?
| (String name) { (String) -> String } -> String
Deletes the environment variable with name if it exists and returns its value: ENV = β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: 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): ENV = β0β ENV.delete(βfooβ) { |name| raise βignoredβ } # => β0β
Raises an exception if name is invalid. See Invalid Names and Values.
() → ::Enumerator[[ String, String ], self]
() { (String name, String value) → boolish } → self
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/rbs/unnamed/env_class.rbs, line 463
def delete_if: () -> ::Enumerator[[ String, String ], self]
| () { (String name, String value) -> boolish } -> self
Yields each environment variable name and its value as a 2-element Array, deleting each environment variable for which the block returns a truthy value, and returning ENV (regardless of whether any deletions): ENV.replace(βfooβ => β0β, βbarβ => β1β, βbazβ => β2β) ENV.delete_if { |name, value| name.start_with?(βbβ) } # => ENV ENV # => {βfooβ=>β0β} ENV.delete_if { |name, value| name.start_with?(βbβ) } # => ENV
Returns an Enumerator if no block given: ENV.replace(βfooβ => β0β, βbarβ => β1β, βbazβ => β2β) e = ENV.delete_if # => #<Enumerator: {βbarβ=>β1β, βbazβ=>β2β, βfooβ=>β0β}:delete_if!> e.each { |name, value| name.start_with?(βbβ) } # => ENV ENV # => {βfooβ=>β0β} e.each { |name, value| name.start_with?(βbβ) } # => ENV
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/rbs/unnamed/env_class.rbs, line 344
def each: () -> ::Enumerator[[ String, String ], self]
| () { ([ String, String ]) -> void } -> self
Yields each environment variable name and its value as a 2-element Array: h = {} ENV.each_pair { |name, value| h = value } # => ENV h # => {βbarβ=>β1β, βfooβ=>β0β}
Returns an Enumerator if no block given: h = {} e = ENV.each_pair # => #<Enumerator: {βbarβ=>β1β, βfooβ=>β0β}:each_pair> e.each { |name, value| h = value } # => ENV h # => {βbarβ=>β1β, βfooβ=>β0β}
() → ::Enumerator[[ String ], self]
() { (String name) → void } → self
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/rbs/unnamed/env_class.rbs, line 387
def each_key: () -> ::Enumerator[[ String ], self]
| () { (String name) -> void } -> self
Yields each environment variable name: ENV.replace(βfooβ => β0β, βbarβ => β1β) # => ENV names = [] ENV.each_key { |name| names.push(name) } # => ENV names # => [βbarβ, βfooβ]
Returns an Enumerator if no block given: e = ENV.each_key # => #<Enumerator: {βbarβ=>β1β, βfooβ=>β0β}:each_key> names = [] e.each { |name| names.push(name) } # => ENV names # => [βbarβ, βfooβ]
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/rbs/unnamed/env_class.rbs, line 366
def each_pair: () -> ::Enumerator[[ String, String ], self]
| () { ([ String, String ]) -> void } -> self
Yields each environment variable name and its value as a 2-element Array: h = {} ENV.each_pair { |name, value| h = value } # => ENV h # => {βbarβ=>β1β, βfooβ=>β0β}
Returns an Enumerator if no block given: h = {} e = ENV.each_pair # => #<Enumerator: {βbarβ=>β1β, βfooβ=>β0β}:each_pair> e.each { |name, value| h = value } # => ENV h # => {βbarβ=>β1β, βfooβ=>β0β}
() → ::Enumerator[[ String ], self]
() { (String value) → void } → self
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/rbs/unnamed/env_class.rbs, line 408
def each_value: () -> ::Enumerator[[ String ], self]
| () { (String value) -> void } -> self
Yields each environment variable value: ENV.replace(βfooβ => β0β, βbarβ => β1β) # => ENV values = [] ENV.each_value { |value| values.push(value) } # => ENV values # => [β1β, β0β]
Returns an Enumerator if no block given: e = ENV.each_value # => #<Enumerator: {βbarβ=>β1β, βfooβ=>β0β}:each_value> values = [] e.each { |value| values.push(value) } # => ENV values # => [β1β, β0β]
() → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/rbs/unnamed/env_class.rbs, line 934
def empty?: () -> bool
Returns true when there are no environment variables, false otherwise: ENV.clear ENV.empty? # => true ENV = β0β ENV.empty? # => false
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/rbs/unnamed/env_class.rbs, line 514
def except: (*String names) -> ::Hash[String, String]
Returns a hash except the given keys from ENV and their values.
ENV #=> {"LANG"=>"en_US.UTF-8", "TERM"=>"xterm-256color", "HOME"=>"/Users/rhc"} ENV.except("TERM","HOME") #=> {"LANG"=>"en_US.UTF-8"}
(String name) → String
[X] (String name, X default) → (String | X)
[X] (String name) { (String) → X } → (String | X)
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/rbs/unnamed/env_class.rbs, line 221
def fetch: (String name) -> String
| [X] (String name, X default) -> (String | X)
| [X] (String name) { (String) -> X } -> (String | X)
If name is the name of an environment variable, returns its value: ENV = β0β ENV.fetch(βfooβ) # => β0β
Otherwise if a block is given (but not a default value), yields name to the block and returns the blockβs return value: ENV.fetch(βfooβ) { |name| :need_not_return_a_string } # => :need_not_return_a_string
Otherwise if a default value is given (but not a block), returns the default value: ENV.delete(βfooβ) ENV.fetch(βfooβ, :default_need_not_be_a_string) # => :default_need_not_be_a_string
If the environment variable does not exist and both default and block are given, issues a warning (βwarning: block supersedes default value argumentβ), yields name to the block, and returns the blockβs return value: ENV.fetch(βfooβ, :default) { |name| :block_return } # => :block_return
Raises KeyError if name is valid, but not found, and neither default value nor block is given: ENV.fetch(βfooβ) # Raises KeyError (key not found: βfooβ)
Raises an exception if name is invalid. See Invalid Names and Values.
Yields each environment variable name and its value as a 2-element Array, returning a Hash of the names and values for which the block returns a truthy value: ENV.replace(βfooβ => β0β, βbarβ => β1β, βbazβ => β2β) ENV.select { |name, value| name.start_with?(βbβ) } # => {βbarβ=>β1β, βbazβ=>β2β} ENV.filter { |name, value| name.start_with?(βbβ) } # => {βbarβ=>β1β, βbazβ=>β2β}
Returns an Enumerator if no block given: e = ENV.select # => #<Enumerator: {βbarβ=>β1β, βbazβ=>β2β, βfooβ=>β0β}:select> e.each { |name, value | name.start_with?(βbβ) } # => {βbarβ=>β1β, βbazβ=>β2β} e = ENV.filter # => #<Enumerator: {βbarβ=>β1β, βbazβ=>β2β, βfooβ=>β0β}:filter> e.each { |name, value | name.start_with?(βbβ) } # => {βbarβ=>β1β, βbazβ=>β2β}
Yields each environment variable name and its value as a 2-element Array, deleting each entry for which the block returns false or nil, and returning ENV if any deletions made, or nil otherwise:
ENV.replace('foo' => '0', 'bar' => '1', 'baz' => '2') ENV.select! { |name, value| name.start_with?('b') } # => ENV ENV # => {"bar"=>"1", "baz"=>"2"} ENV.select! { |name, value| true } # => nil ENV.replace('foo' => '0', 'bar' => '1', 'baz' => '2') ENV.filter! { |name, value| name.start_with?('b') } # => ENV ENV # => {"bar"=>"1", "baz"=>"2"} ENV.filter! { |name, value| true } # => nil
Returns an Enumerator if no block given:
ENV.replace('foo' => '0', 'bar' => '1', 'baz' => '2') e = ENV.select! # => #<Enumerator: {"bar"=>"1", "baz"=>"2"}:select!> e.each { |name, value| name.start_with?('b') } # => ENV ENV # => {"bar"=>"1", "baz"=>"2"} e.each { |name, value| true } # => nil ENV.replace('foo' => '0', 'bar' => '1', 'baz' => '2') e = ENV.filter! # => #<Enumerator: {"bar"=>"1", "baz"=>"2"}:filter!> e.each { |name, value| name.start_with?('b') } # => ENV ENV # => {"bar"=>"1", "baz"=>"2"} e.each { |name, value| true } # => nil
Returns true if there is an environment variable with the given name: ENV.replace(βfooβ => β0β, βbarβ => β1β) ENV.include?(βfooβ) # => true
Returns false if name is a valid String and there is no such environment variable: ENV.include?(βbazβ) # => false
Returns false if name is the empty String or is a String containing character '=': ENV.include?(β) # => false ENV.include?(β=β) # => false
Raises an exception if name is a String containing the NUL character "\0": ENV.include?(β\0β) # Raises ArgumentError (bad environment variable name: contains null byte)
Raises an exception if name has an encoding that is not ASCII-compatible: ENV.include?(β\xa1\xa1β.force_encoding(Encoding::UTF_16LE)) # Raises ArgumentError (bad environment variable name: ASCII incompatible encoding: UTF-16LE)
Raises an exception if name is not a String: ENV.include?(Object.new) # TypeError (no implicit conversion of Object into String)
(String value) → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/rbs/unnamed/env_class.rbs, line 1104
def has_value?: (String value) -> bool
Returns true if value is the value for some environment variable name, false otherwise: ENV.replace(βfooβ => β0β, βbarβ => β1β) ENV.value?(β0β) # => true ENV.has_value?(β0β) # => true ENV.value?(β2β) # => false ENV.has_value?(β2β) # => false
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/rbs/unnamed/env_class.rbs, line 1020
def include?: (String name) -> bool
Returns true if there is an environment variable with the given name: ENV.replace(βfooβ => β0β, βbarβ => β1β) ENV.include?(βfooβ) # => true
Returns false if name is a valid String and there is no such environment variable: ENV.include?(βbazβ) # => false
Returns false if name is the empty String or is a String containing character '=': ENV.include?(β) # => false ENV.include?(β=β) # => false
Raises an exception if name is a String containing the NUL character "\0": ENV.include?(β\0β) # Raises ArgumentError (bad environment variable name: contains null byte)
Raises an exception if name has an encoding that is not ASCII-compatible: ENV.include?(β\xa1\xa1β.force_encoding(Encoding::UTF_16LE)) # Raises ArgumentError (bad environment variable name: ASCII incompatible encoding: UTF-16LE)
Raises an exception if name is not a String: ENV.include?(Object.new) # TypeError (no implicit conversion of Object into String)
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/rbs/unnamed/env_class.rbs, line 736
def invert: () -> ::Hash[String, String]
Returns a Hash whose keys are the ENV values, and whose values are the corresponding ENV names: ENV.replace(βfooβ => β0β, βbarβ => β1β) ENV.invert # => {β1β=>βbarβ, β0β=>βfooβ}
For a duplicate ENV value, overwrites the hash entry: ENV.replace(βfooβ => β0β, βbarβ => β0β) ENV.invert # => {β0β=>βfooβ}
Note that the order of the ENV processing is OS-dependent, which means that the order of overwriting is also OS-dependent. See About Ordering.
() → ::Enumerator[[ String, String ], self]
() { (String name, String value) → boolish } → self
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/rbs/unnamed/env_class.rbs, line 485
def keep_if: () -> ::Enumerator[[ String, String ], self]
| () { (String name, String value) -> boolish } -> self
Yields each environment variable name and its value as a 2-element Array, deleting each environment variable for which the block returns false or nil, and returning ENV: ENV.replace(βfooβ => β0β, βbarβ => β1β, βbazβ => β2β) ENV.keep_if { |name, value| name.start_with?(βbβ) } # => ENV ENV # => {βbarβ=>β1β, βbazβ=>β2β}
Returns an Enumerator if no block given: ENV.replace(βfooβ => β0β, βbarβ => β1β, βbazβ => β2β) e = ENV.keep_if # => #<Enumerator: {βbarβ=>β1β, βbazβ=>β2β, βfooβ=>β0β}:keep_if> e.each { |name, value| name.start_with?(βbβ) } # => ENV ENV # => {βbarβ=>β1β, βbazβ=>β2β}
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/rbs/unnamed/env_class.rbs, line 895
def key: (String value) -> String?
Returns the name of the first environment variable with value, if it exists: ENV.replace(βfooβ => β0β, βbarβ => β0β) ENV.key(β0β) # => βfooβ
The order in which environment variables are examined is OS-dependent. See About Ordering.
Returns nil if there is no such value.
Raises an exception if value is invalid: ENV.key(Object.new) # raises TypeError (no implicit conversion of Object into String)
See Invalid Names and Values.
Returns true if there is an environment variable with the given name: ENV.replace(βfooβ => β0β, βbarβ => β1β) ENV.include?(βfooβ) # => true
Returns false if name is a valid String and there is no such environment variable: ENV.include?(βbazβ) # => false
Returns false if name is the empty String or is a String containing character '=': ENV.include?(β) # => false ENV.include?(β=β) # => false
Raises an exception if name is a String containing the NUL character "\0": ENV.include?(β\0β) # Raises ArgumentError (bad environment variable name: contains null byte)
Raises an exception if name has an encoding that is not ASCII-compatible: ENV.include?(β\xa1\xa1β.force_encoding(Encoding::UTF_16LE)) # Raises ArgumentError (bad environment variable name: ASCII incompatible encoding: UTF-16LE)
Raises an exception if name is not a String: ENV.include?(Object.new) # TypeError (no implicit conversion of Object into String)
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/rbs/unnamed/env_class.rbs, line 950
def keys: () -> ::Array[String]
Returns all variable names in an Array: ENV.replace(βfooβ => β0β, βbarβ => β1β) ENV.keys # => [βbarβ, βfooβ]
The order of the names is OS-dependent. See About Ordering.
Returns the empty Array if ENV is empty.
Returns the count of environment variables: ENV.replace(βfooβ => β0β, βbarβ => β1β) ENV.length # => 2 ENV.size # => 2
Returns true if there is an environment variable with the given name: ENV.replace(βfooβ => β0β, βbarβ => β1β) ENV.include?(βfooβ) # => true
Returns false if name is a valid String and there is no such environment variable: ENV.include?(βbazβ) # => false
Returns false if name is the empty String or is a String containing character '=': ENV.include?(β) # => false ENV.include?(β=β) # => false
Raises an exception if name is a String containing the NUL character "\0": ENV.include?(β\0β) # Raises ArgumentError (bad environment variable name: contains null byte)
Raises an exception if name has an encoding that is not ASCII-compatible: ENV.include?(β\xa1\xa1β.force_encoding(Encoding::UTF_16LE)) # Raises ArgumentError (bad environment variable name: ASCII incompatible encoding: UTF-16LE)
Raises an exception if name is not a String: ENV.include?(Object.new) # TypeError (no implicit conversion of Object into String)
Adds to ENV each key/value pair in the given hash; returns ENV: ENV.replace(βfooβ => β0β, βbarβ => β1β) ENV.merge!(βbazβ => β2β, βbatβ => β3β) # => {βbarβ=>β1β, βbatβ=>β3β, βbazβ=>β2β, βfooβ=>β0β}
Deletes the ENV entry for a hash value that is nil: ENV.merge!(βbazβ => nil, βbatβ => nil) # => {βbarβ=>β1β, βfooβ=>β0β}
For an already-existing name, if no block given, overwrites the ENV value: ENV.merge!(βfooβ => β4β) # => {βbarβ=>β1β, βfooβ=>β4β}
For an already-existing name, if block given, yields the name, its ENV value, and its hash value; the blockβs return value becomes the new name: ENV.merge!(βfooβ => β5β) { |name, env_val, hash_val | env_val + hash_val } # => {βbarβ=>β1β, βfooβ=>β45β}
Raises an exception if a name or value is invalid (see Invalid Names and Values); ENV.replace(βfooβ => β0β, βbarβ => β1β) ENV.merge!(βfooβ => β6β, :bar => β7β, βbazβ => β9β) # Raises TypeError (no implicit conversion of Symbol into String) ENV # => {βbarβ=>β1β, βfooβ=>β6β} ENV.merge!(βfooβ => β7β, βbarβ => 8, βbazβ => β9β) # Raises TypeError (no implicit conversion of Integer into String) ENV # => {βbarβ=>β1β, βfooβ=>β7β}
Raises an exception if the block returns an invalid name: (see Invalid Names and Values): ENV.merge!(βbatβ => β8β, βfooβ => β9β) { |name, env_val, hash_val | 10 } # Raises TypeError (no implicit conversion of Integer into String) ENV # => {βbarβ=>β1β, βbatβ=>β8β, βfooβ=>β7β}
Note that for the exceptions above, hash pairs preceding an invalid name or value are processed normally; those following are ignored.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/rbs/unnamed/env_class.rbs, line 1211
def rassoc: (String value) -> [ String, String ]?
Returns a 2-element Array containing the name and value of the first found environment variable that has value value, if one exists: ENV.replace(βfooβ => β0β, βbarβ => β0β) ENV.rassoc(β0β) # => [βbarβ, β0β]
The order in which environment variables are examined is OS-dependent. See About Ordering.
Returns nil if there is no such environment variable.
() → nil
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/rbs/unnamed/env_class.rbs, line 852
def rehash: () -> nil
(Provided for compatibility with Hash.)
Does not modify ENV; returns nil.
() → ::Enumerator[[ String, String ], self]
() { (String name, String value) → boolish } → self
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/rbs/unnamed/env_class.rbs, line 546
def reject: () -> ::Enumerator[[ String, String ], self]
| () { (String name, String value) -> boolish } -> self
Yields each environment variable name and its value as a 2-element Array. Returns a Hash whose items are determined by the block. When the block returns a truthy value, the name/value pair is added to the return Hash; otherwise the pair is ignored: ENV.replace(βfooβ => β0β, βbarβ => β1β, βbazβ => β2β) ENV.reject { |name, value| name.start_with?(βbβ) } # => {βfooβ=>β0β}
Returns an Enumerator if no block given: e = ENV.reject e.each { |name, value| name.start_with?(βbβ) } # => {βfooβ=>β0β}
() → ::Enumerator[[ String, String ], self?]
() { (String name, String value) → boolish } → self?
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/rbs/unnamed/env_class.rbs, line 572
def reject!: () -> ::Enumerator[[ String, String ], self?]
| () { (String name, String value) -> boolish } -> self?
Similar to ENV.delete_if, but returns nil if no changes were made.
Yields each environment variable name and its value as a 2-element Array, deleting each environment variable for which the block returns a truthy value, and returning ENV (if any deletions) or nil (if not): ENV.replace(βfooβ => β0β, βbarβ => β1β, βbazβ => β2β) ENV.reject! { |name, value| name.start_with?(βbβ) } # => ENV ENV # => {βfooβ=>β0β} ENV.reject! { |name, value| name.start_with?(βbβ) } # => nil
Returns an Enumerator if no block given: ENV.replace(βfooβ => β0β, βbarβ => β1β, βbazβ => β2β) e = ENV.reject! # => #<Enumerator: {βbarβ=>β1β, βbazβ=>β2β, βfooβ=>β0β}:reject!> e.each { |name, value| name.start_with?(βbβ) } # => ENV ENV # => {βfooβ=>β0β} e.each { |name, value| name.start_with?(βbβ) } # => nil
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/rbs/unnamed/env_class.rbs, line 756
def replace: (Hash[String, String]) -> self
Replaces the entire content of the environment variables with the name/value pairs in the given hash; returns ENV.
Replaces the content of ENV with the given pairs: ENV.replace(βfooβ => β0β, βbarβ => β1β) # => ENV ENV.to_hash # => {βbarβ=>β1β, βfooβ=>β0β}
Raises an exception if a name or value is invalid (see Invalid Names and Values): ENV.replace(βfooβ => β0β, :bar => β1β) # Raises TypeError (no implicit conversion of Symbol into String) ENV.replace(βfooβ => β0β, βbarβ => 1) # Raises TypeError (no implicit conversion of Integer into String) ENV.to_hash # => {βbarβ=>β1β, βfooβ=>β0β}
() → ::Enumerator[[ String, String ], ::Hash[String, String]]
() { (String name, String value) → boolish } → ::Hash[String, String]
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/rbs/unnamed/env_class.rbs, line 596
def select: () -> ::Enumerator[[ String, String ], ::Hash[String, String]]
| () { (String name, String value) -> boolish } -> ::Hash[String, String]
Yields each environment variable name and its value as a 2-element Array, returning a Hash of the names and values for which the block returns a truthy value: ENV.replace(βfooβ => β0β, βbarβ => β1β, βbazβ => β2β) ENV.select { |name, value| name.start_with?(βbβ) } # => {βbarβ=>β1β, βbazβ=>β2β} ENV.filter { |name, value| name.start_with?(βbβ) } # => {βbarβ=>β1β, βbazβ=>β2β}
Returns an Enumerator if no block given: e = ENV.select # => #<Enumerator: {βbarβ=>β1β, βbazβ=>β2β, βfooβ=>β0β}:select> e.each { |name, value | name.start_with?(βbβ) } # => {βbarβ=>β1β, βbazβ=>β2β} e = ENV.filter # => #<Enumerator: {βbarβ=>β1β, βbazβ=>β2β, βfooβ=>β0β}:filter> e.each { |name, value | name.start_with?(βbβ) } # => {βbarβ=>β1β, βbazβ=>β2β}
() → ::Enumerator[[ String, String ], self?]
() { (String name, String value) → boolish } → self?
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/rbs/unnamed/env_class.rbs, line 658
def select!: () -> ::Enumerator[[ String, String ], self?]
| () { (String name, String value) -> boolish } -> self?
Yields each environment variable name and its value as a 2-element Array, deleting each entry for which the block returns false or nil, and returning ENV if any deletions made, or nil otherwise:
ENV.replace('foo' => '0', 'bar' => '1', 'baz' => '2') ENV.select! { |name, value| name.start_with?('b') } # => ENV ENV # => {"bar"=>"1", "baz"=>"2"} ENV.select! { |name, value| true } # => nil ENV.replace('foo' => '0', 'bar' => '1', 'baz' => '2') ENV.filter! { |name, value| name.start_with?('b') } # => ENV ENV # => {"bar"=>"1", "baz"=>"2"} ENV.filter! { |name, value| true } # => nil
Returns an Enumerator if no block given:
ENV.replace('foo' => '0', 'bar' => '1', 'baz' => '2') e = ENV.select! # => #<Enumerator: {"bar"=>"1", "baz"=>"2"}:select!> e.each { |name, value| name.start_with?('b') } # => ENV ENV # => {"bar"=>"1", "baz"=>"2"} e.each { |name, value| true } # => nil ENV.replace('foo' => '0', 'bar' => '1', 'baz' => '2') e = ENV.filter! # => #<Enumerator: {"bar"=>"1", "baz"=>"2"}:filter!> e.each { |name, value| name.start_with?('b') } # => ENV ENV # => {"bar"=>"1", "baz"=>"2"} e.each { |name, value| true } # => nil
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/rbs/unnamed/env_class.rbs, line 716
def shift: () -> [ String, String ]?
Removes the first environment variable from ENV and returns a 2-element Array containing its name and value: ENV.replace(βfooβ => β0β, βbarβ => β1β) ENV.to_hash # => {βbarβ => β1β, βfooβ => β0β} ENV.shift # => [βbarβ, β1β] ENV.to_hash # => {βfooβ => β0β}
Exactly which environment variable is βfirstβ is OS-dependent. See About Ordering.
Returns nil if the environment is empty.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/rbs/unnamed/env_class.rbs, line 908
def size: () -> Integer
Returns the count of environment variables: ENV.replace(βfooβ => β0β, βbarβ => β1β) ENV.length # => 2 ENV.size # => 2
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/rbs/unnamed/env_class.rbs, line 502
def slice: (*String names) -> ::Hash[String, String]
Returns a Hash of the given ENV names and their corresponding values: ENV.replace(βfooβ => β0β, βbarβ => β1β, βbazβ => β2β, βbatβ => β3β) ENV.slice(βfooβ, βbazβ) # => {βfooβ=>β0β, βbazβ=>β2β} ENV.slice(βbazβ, βfooβ) # => {βbazβ=>β2β, βfooβ=>β0β}
Raises an exception if any of the names is invalid (see Invalid Names and Values): ENV.slice(βfooβ, βbarβ, :bat) # Raises TypeError (no implicit conversion of Symbol into String)
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. ENV.clear ENV = nil # => nil ENV.include?('foo') # => false ENV.store('bar', nil) # => nil ENV.include?('bar') # => false -
If
valueisnil, does nothing. ENV.clear ENV = nil # => nil ENV.include?('foo') # => false ENV.store('bar', nil) # => nil ENV.include?('bar') # => false -
If
valueis notnil, creates the environment variable withnameandvalue: # Create 'foo' using ENV.[]=. ENV = '0' # => '0' ENV # => '0' # Create 'bar' using ENV.store. ENV.store('bar', '1') # => '1' ENV # => '1'
-
-
If the named environment variable exists:
-
If
valueis notnil, updates the environment variable with valuevalue: # Update 'foo' using ENV.[]=. ENV = '2' # => '2' ENV # => '2' # Update 'bar' using ENV.store. ENV.store('bar', '3') # => '3' ENV # => '3' -
If
valueisnil, deletes the environment variable: # Delete 'foo' using ENV.[]=. ENV = 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.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/rbs/unnamed/env_class.rbs, line 864
def to_a: () -> ::Array[[ String, String ]]
Returns the contents of ENV as an Array of 2-element Arrays, each of which is a name/value pair: ENV.replace(βfooβ => β0β, βbarβ => β1β) ENV.to_a # => [[βbarβ, β1β], [βfooβ, β0β]]
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/rbs/unnamed/env_class.rbs, line 1165
def to_hash: () -> ::Hash[String, String]
Returns a Hash containing all name/value pairs from ENV: ENV.replace(βfooβ => β0β, βbarβ => β1β) ENV.to_hash # => {βbarβ=>β1β, βfooβ=>β0β}
() → "ENV"
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/rbs/unnamed/env_class.rbs, line 874
def to_s: () -> "ENV"
Returns String βENVβ: ENV.to_s # => βENVβ
(Hash[String, String?]) → self
(Hash[String, String?]) { (String name, String env_val, String? hash_val) → String } → self
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/rbs/unnamed/env_class.rbs, line 798
def update: (Hash[String, String?]) -> self
| (Hash[String, String?]) { (String name, String env_val, String? hash_val) -> String } -> self
Adds to ENV each key/value pair in the given hash; returns ENV: ENV.replace(βfooβ => β0β, βbarβ => β1β) ENV.merge!(βbazβ => β2β, βbatβ => β3β) # => {βbarβ=>β1β, βbatβ=>β3β, βbazβ=>β2β, βfooβ=>β0β}
Deletes the ENV entry for a hash value that is nil: ENV.merge!(βbazβ => nil, βbatβ => nil) # => {βbarβ=>β1β, βfooβ=>β0β}
For an already-existing name, if no block given, overwrites the ENV value: ENV.merge!(βfooβ => β4β) # => {βbarβ=>β1β, βfooβ=>β4β}
For an already-existing name, if block given, yields the name, its ENV value, and its hash value; the blockβs return value becomes the new name: ENV.merge!(βfooβ => β5β) { |name, env_val, hash_val | env_val + hash_val } # => {βbarβ=>β1β, βfooβ=>β45β}
Raises an exception if a name or value is invalid (see Invalid Names and Values); ENV.replace(βfooβ => β0β, βbarβ => β1β) ENV.merge!(βfooβ => β6β, :bar => β7β, βbazβ => β9β) # Raises TypeError (no implicit conversion of Symbol into String) ENV # => {βbarβ=>β1β, βfooβ=>β6β} ENV.merge!(βfooβ => β7β, βbarβ => 8, βbazβ => β9β) # Raises TypeError (no implicit conversion of Integer into String) ENV # => {βbarβ=>β1β, βfooβ=>β7β}
Raises an exception if the block returns an invalid name: (see Invalid Names and Values): ENV.merge!(βbatβ => β8β, βfooβ => β9β) { |name, env_val, hash_val | 10 } # Raises TypeError (no implicit conversion of Integer into String) ENV # => {βbarβ=>β1β, βbatβ=>β8β, βfooβ=>β7β}
Note that for the exceptions above, hash pairs preceding an invalid name or value are processed normally; those following are ignored.
Returns true if value is the value for some environment variable name, false otherwise: ENV.replace(βfooβ => β0β, βbarβ => β1β) ENV.value?(β0β) # => true ENV.has_value?(β0β) # => true ENV.value?(β2β) # => false ENV.has_value?(β2β) # => false
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/rbs/unnamed/env_class.rbs, line 966
def values: () -> ::Array[String]
Returns all environment variable values in an Array: ENV.replace(βfooβ => β0β, βbarβ => β1β) ENV.values # => [β1β, β0β]
The order of the values is OS-dependent. See About Ordering.
Returns the empty Array if ENV is empty.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/rbs/unnamed/env_class.rbs, line 986
def values_at: (*String names) -> ::Array[String?]
Returns an Array containing the environment variable values associated with the given names: ENV.replace(βfooβ => β0β, βbarβ => β1β, βbazβ => β2β) ENV.values_at(βfooβ, βbazβ) # => [β0β, β2β]
Returns nil in the Array for each name that is not an ENV name: ENV.values_at(βfooβ, βbatβ, βbarβ, βbamβ) # => [β0β, nil, β1β, nil]
Returns an empty Array if no names given.
Raises an exception if any name is invalid. See Invalid Names and Values.