Assigns a new value to the hash:
hash = HashWithIndifferentAccess.new hash[:key] = "value"
# File lib/active_support/hash_with_indifferent_access.rb, line 54 54: def []=(key, value) 55: regular_writer(convert_key(key), convert_value(value)) 56: end
# File lib/active_support/hash_with_indifferent_access.rb, line 32 32: def default(key = nil) 33: if key.is_a?(Symbol) && include?(key = key.to_s) 34: self[key] 35: else 36: super 37: end 38: end
Removes a specified key from the hash.
# File lib/active_support/hash_with_indifferent_access.rb, line 146 146: def delete(key) 147: super(convert_key(key)) 148: end
Returns an exact copy of the hash.
# File lib/active_support/hash_with_indifferent_access.rb, line 123 123: def dup 124: self.class.new(self).tap do |new_hash| 125: new_hash.default = default 126: end 127: end
Always returns true, so that Array#extract_options! finds members of this class.
# File lib/active_support/hash_with_indifferent_access.rb, line 11 11: def extractable_options? 12: true 13: end
Same as Hash#fetch where the key passed as argument can be either a string or a symbol:
counters = HashWithIndifferentAccess.new counters[:foo] = 1 counters.fetch("foo") # => 1 counters.fetch(:bar, 0) # => 0 counters.fetch(:bar) {|key| 0} # => 0 counters.fetch(:zoo) # => KeyError: key not found: "zoo"
# File lib/active_support/hash_with_indifferent_access.rb, line 107 107: def fetch(key, *extras) 108: super(convert_key(key), *extras) 109: 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 88 88: def key?(key) 89: super(convert_key(key)) 90: 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 131 131: def merge(hash) 132: self.dup.update(hash) 133: end
# File lib/active_support/hash_with_indifferent_access.rb, line 19 19: def nested_under_indifferent_access 20: self 21: 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 137 137: def reverse_merge(other_hash) 138: super self.class.new_from_hash_copying_default(other_hash) 139: end
# File lib/active_support/hash_with_indifferent_access.rb, line 141 141: def reverse_merge!(other_hash) 142: replace(reverse_merge( other_hash )) 143: end
# File lib/active_support/hash_with_indifferent_access.rb, line 151 151: def stringify_keys; dup end
# File lib/active_support/hash_with_indifferent_access.rb, line 150 150: def stringify_keys!; self end
# File lib/active_support/hash_with_indifferent_access.rb, line 153 153: def symbolize_keys; to_hash.symbolize_keys end
# File lib/active_support/hash_with_indifferent_access.rb, line 154 154: 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 70 70: def update(other_hash) 71: if other_hash.is_a? HashWithIndifferentAccess 72: super(other_hash) 73: else 74: other_hash.each_pair { |key, value| regular_writer(convert_key(key), convert_value(value)) } 75: self 76: end 77: 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 118 118: def values_at(*indices) 119: indices.collect {|key| self[convert_key(key)]} 120: end
# File lib/active_support/hash_with_indifferent_access.rb, line 162 162: def convert_key(key) 163: key.kind_of?(Symbol) ? key.to_s : key 164: end
# File lib/active_support/hash_with_indifferent_access.rb, line 166 166: def convert_value(value) 167: if value.is_a? Hash 168: value.nested_under_indifferent_access 169: elsif value.is_a?(Array) 170: value.dup.replace(value.map { |e| convert_value(e) }) 171: else 172: value 173: end 174: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.