Class HashWithIndifferentAccess
In: vendor/rails/activesupport/lib/active_support/core_ext/hash/indifferent_access.rb
Parent: Hash

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.

Methods

External Aliases

[]= -> regular_writer

Public Class methods

[Source]

    # File vendor/rails/activesupport/lib/active_support/core_ext/hash/indifferent_access.rb, line 6
 6:   def initialize(constructor = {})
 7:     if constructor.is_a?(Hash)
 8:       super()
 9:       update(constructor)
10:     else
11:       super(constructor)
12:     end
13:   end

Public Instance methods

Assigns a new value to the hash:

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

[Source]

    # File vendor/rails/activesupport/lib/active_support/core_ext/hash/indifferent_access.rb, line 31
31:   def []=(key, value)
32:     regular_writer(convert_key(key), convert_value(value))
33:   end

[Source]

     # File vendor/rails/activesupport/lib/active_support/core_ext/hash/indifferent_access.rb, line 109
109:     def convert_key(key)
110:       key.kind_of?(Symbol) ? key.to_s : key
111:     end

[Source]

     # File vendor/rails/activesupport/lib/active_support/core_ext/hash/indifferent_access.rb, line 113
113:     def convert_value(value)
114:       case value
115:       when Hash
116:         value.with_indifferent_access
117:       when Array
118:         value.collect { |e| e.is_a?(Hash) ? e.with_indifferent_access : e }
119:       else
120:         value
121:       end
122:     end

[Source]

    # File vendor/rails/activesupport/lib/active_support/core_ext/hash/indifferent_access.rb, line 15
15:   def default(key = nil)
16:     if key.is_a?(Symbol) && include?(key = key.to_s)
17:       self[key]
18:     else
19:       super
20:     end
21:   end

Removes a specified key from the hash.

[Source]

    # File vendor/rails/activesupport/lib/active_support/core_ext/hash/indifferent_access.rb, line 95
95:   def delete(key)
96:     super(convert_key(key))
97:   end

Returns an exact copy of the hash.

[Source]

    # File vendor/rails/activesupport/lib/active_support/core_ext/hash/indifferent_access.rb, line 84
84:   def dup
85:     HashWithIndifferentAccess.new(self)
86:   end

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

[Source]

    # File vendor/rails/activesupport/lib/active_support/core_ext/hash/indifferent_access.rb, line 68
68:   def fetch(key, *extras)
69:     super(convert_key(key), *extras)
70:   end

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

[Source]

    # File vendor/rails/activesupport/lib/active_support/core_ext/hash/indifferent_access.rb, line 59
59:   def key?(key)
60:     super(convert_key(key))
61:   end

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

[Source]

    # File vendor/rails/activesupport/lib/active_support/core_ext/hash/indifferent_access.rb, line 90
90:   def merge(hash)
91:     self.dup.update(hash)
92:   end

[Source]

    # File vendor/rails/activesupport/lib/active_support/core_ext/hash/indifferent_access.rb, line 99
99:   def stringify_keys!; self end

[Source]

     # File vendor/rails/activesupport/lib/active_support/core_ext/hash/indifferent_access.rb, line 100
100:   def symbolize_keys!; self end

Convert to a Hash with String keys.

[Source]

     # File vendor/rails/activesupport/lib/active_support/core_ext/hash/indifferent_access.rb, line 104
104:   def to_hash
105:     Hash.new(default).merge(self)
106:   end

[Source]

     # File vendor/rails/activesupport/lib/active_support/core_ext/hash/indifferent_access.rb, line 101
101:   def to_options!; self end

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!"}

[Source]

    # File vendor/rails/activesupport/lib/active_support/core_ext/hash/indifferent_access.rb, line 45
45:   def update(other_hash)
46:     other_hash.each_pair { |key, value| regular_writer(convert_key(key), convert_value(value)) }
47:     self
48:   end

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"]

[Source]

    # File vendor/rails/activesupport/lib/active_support/core_ext/hash/indifferent_access.rb, line 79
79:   def values_at(*indices)
80:     indices.collect {|key| self[convert_key(key)]}
81:   end

[Validate]