Assigns a new value to the hash:
hash = HashWithIndifferentAccess.new hash[:key] = "value"
# File lib/active_support/hash_with_indifferent_access.rb, line 44
44: def []=(key, value)
45: regular_writer(convert_key(key), convert_value(value))
46: end
# File lib/active_support/hash_with_indifferent_access.rb, line 22
22: def default(key = nil)
23: if key.is_a?(Symbol) && include?(key = key.to_s)
24: self[key]
25: else
26: super
27: end
28: end
Removes a specified key from the hash.
# File lib/active_support/hash_with_indifferent_access.rb, line 120
120: def delete(key)
121: super(convert_key(key))
122: end
Returns an exact copy of the hash.
# File lib/active_support/hash_with_indifferent_access.rb, line 99
99: def dup
100: HashWithIndifferentAccess.new(self)
101: end
# File lib/active_support/hash_with_indifferent_access.rb, line 9
9: def extractable_options?
10: true
11: end
Fetches the value for the specified key, same as doing hash[key]
# File lib/active_support/hash_with_indifferent_access.rb, line 83
83: def fetch(key, *extras)
84: super(convert_key(key), *extras)
85: 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
# File lib/active_support/hash_with_indifferent_access.rb, line 74
74: def key?(key)
75: super(convert_key(key))
76: end
Merges the instantized and the specified hashes together, giving precedence to the values from the second hash Does not overwrite the existing hash.
# File lib/active_support/hash_with_indifferent_access.rb, line 105
105: def merge(hash)
106: self.dup.update(hash)
107: 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.
# File lib/active_support/hash_with_indifferent_access.rb, line 111
111: def reverse_merge(other_hash)
112: super self.class.new_from_hash_copying_default(other_hash)
113: end
# File lib/active_support/hash_with_indifferent_access.rb, line 115
115: def reverse_merge!(other_hash)
116: replace(reverse_merge( other_hash ))
117: end
# File lib/active_support/hash_with_indifferent_access.rb, line 125
125: def stringify_keys; dup end
# File lib/active_support/hash_with_indifferent_access.rb, line 124
124: def stringify_keys!; self end
# File lib/active_support/hash_with_indifferent_access.rb, line 127
127: def symbolize_keys; to_hash.symbolize_keys end
Convert to a Hash with String keys.
# File lib/active_support/hash_with_indifferent_access.rb, line 131
131: def to_hash
132: Hash.new(default).merge!(self)
133: end
# File lib/active_support/hash_with_indifferent_access.rb, line 128
128: 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!"}
# File lib/active_support/hash_with_indifferent_access.rb, line 60
60: def update(other_hash)
61: other_hash.each_pair { |key, value| regular_writer(convert_key(key), convert_value(value)) }
62: self
63: 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"]
# File lib/active_support/hash_with_indifferent_access.rb, line 94
94: def values_at(*indices)
95: indices.collect {|key| self[convert_key(key)]}
96: end
# File lib/active_support/hash_with_indifferent_access.rb, line 136
136: def convert_key(key)
137: key.kind_of?(Symbol) ? key.to_s : key
138: end
# File lib/active_support/hash_with_indifferent_access.rb, line 140
140: def convert_value(value)
141: case value
142: when Hash
143: self.class.new_from_hash_copying_default(value)
144: when Array
145: value.collect { |e| e.is_a?(Hash) ? self.class.new_from_hash_copying_default(e) : e }
146: else
147: value
148: end
149: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.