Class

HashWithIndifferentAccess

Inheritance
< Hash < Object

This class has dubious semantics and we only have it so that people can write params[:key] instead of params[‘key’] and they get the same value for both keys.

Aliases

Method Alias Description
[]= → regular_writer

Methods

Class

Visibility Signature
public new (constructor = {})

Instance

Visibility Signature
public []= (key, value)
public convert_key (key)
public convert_value (value)
public default (key = nil)
public delete (key)
public dup ()
public fetch (key, *extras)
public key? (key)
public merge (hash)
public reverse_merge (other_hash)
public stringify_keys! ()
public symbolize_keys! ()
public to_hash ()
public to_options! ()
public update (other_hash)
public values_at (*indices)

Class Method Detail

new(constructor = {})

Instance Method Detail

[]=(key, value)

Assigns a new value to the hash:

  hash = HashWithIndifferentAccess.new
  hash[:key] = "value"

convert_key(key)

convert_value(value)

default(key = nil)

delete(key)

Removes a specified key from the hash.

dup()

Returns an exact copy of the hash.

fetch(key, *extras)

Fetches the value for the specified key, same as doing hash[key]

key?(key)

Checks the hash for a key matching the argument passed in:

  hash = HashWithIndifferentAccess.new
  hash["key"] = "value"
  hash.key? :key  # => true
  hash.key? "key" # => true

merge(hash)

Merges the instantized and the specified hashes together, giving precedence to the values from the second hash Does not overwrite the existing hash.

reverse_merge(other_hash)

Performs the opposite of merge, with the keys and values from the first hash taking precedence over the second. This overloaded definition prevents returning a regular hash, if reverse_merge is called on a HashWithDifferentAccess.

stringify_keys!()

symbolize_keys!()

to_hash()

Convert to a Hash with String keys.

to_options!()

update(other_hash)

Updates the instantized hash with values from the second:

  hash_1 = HashWithIndifferentAccess.new
  hash_1[:key] = "value"

  hash_2 = HashWithIndifferentAccess.new
  hash_2[:key] = "New Value!"

  hash_1.update(hash_2) # => {"key"=>"New Value!"}

values_at(*indices)

Returns an array of the values at the specified indices:

  hash = HashWithIndifferentAccess.new
  hash[:a] = "x"
  hash[:b] = "y"
  hash.values_at("a", "b") # => ["x", "y"]