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.
- []=
- convert_key
- convert_value
- default
- delete
- dup
- fetch
- key?
- merge
- new
- reverse_merge
- stringify_keys!
- symbolize_keys!
- to_hash
- to_options!
- update
- values_at
[ show source ]
# File 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
Assigns a new value to the hash:
hash = HashWithIndifferentAccess.new hash[:key] = "value"
[ show source ]
# File 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
[ show source ]
# File activesupport/lib/active_support/core_ext/hash/indifferent_access.rb, line 115 115: def convert_key(key) 116: key.kind_of?(Symbol) ? key.to_s : key 117: end
[ show source ]
# File activesupport/lib/active_support/core_ext/hash/indifferent_access.rb, line 119 119: def convert_value(value) 120: case value 121: when Hash 122: value.with_indifferent_access 123: when Array 124: value.collect { |e| e.is_a?(Hash) ? e.with_indifferent_access : e } 125: else 126: value 127: end 128: end
[ show source ]
# File 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.
[ show source ]
# File activesupport/lib/active_support/core_ext/hash/indifferent_access.rb, line 101 101: def delete(key) 102: super(convert_key(key)) 103: end
Returns an exact copy of the hash.
[ show source ]
# File 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]
[ show source ]
# File 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
[ show source ]
# File 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.
[ show source ]
# File activesupport/lib/active_support/core_ext/hash/indifferent_access.rb, line 90 90: def merge(hash) 91: self.dup.update(hash) 92: end
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.
[ show source ]
# File activesupport/lib/active_support/core_ext/hash/indifferent_access.rb, line 96 96: def reverse_merge(other_hash) 97: super other_hash.with_indifferent_access 98: end
[ show source ]
# File activesupport/lib/active_support/core_ext/hash/indifferent_access.rb, line 105 105: def stringify_keys!; self end
[ show source ]
# File activesupport/lib/active_support/core_ext/hash/indifferent_access.rb, line 106 106: def symbolize_keys!; self end
Convert to a Hash with String keys.
[ show source ]
# File activesupport/lib/active_support/core_ext/hash/indifferent_access.rb, line 110 110: def to_hash 111: Hash.new(default).merge(self) 112: end
[ show source ]
# File activesupport/lib/active_support/core_ext/hash/indifferent_access.rb, line 107 107: 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!"}
[ show source ]
# File 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"]
[ show source ]
# File 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