- []=
- convert_key
- convert_value
- default
- delete
- dup
- extractable_options?
- fetch
- key?
- merge
- new
- new_from_hash_copying_default
- reverse_merge
- reverse_merge!
- stringify_keys
- stringify_keys!
- symbolize_keys
- to_hash
- to_options!
- update
- values_at
[ show source ]
# File activesupport/lib/active_support/hash_with_indifferent_access.rb, line 13 13: def initialize(constructor = {}) 14: if constructor.is_a?(Hash) 15: super() 16: update(constructor) 17: else 18: super(constructor) 19: end 20: end
[ show source ]
# File activesupport/lib/active_support/hash_with_indifferent_access.rb, line 30 30: def self.new_from_hash_copying_default(hash) 31: ActiveSupport::HashWithIndifferentAccess.new(hash).tap do |new_hash| 32: new_hash.default = hash.default 33: end 34: end
Assigns a new value to the hash:
hash = HashWithIndifferentAccess.new hash[:key] = "value"
[ show source ]
# File activesupport/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
[ show source ]
# File activesupport/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.
[ show source ]
# File activesupport/lib/active_support/hash_with_indifferent_access.rb, line 118 118: def delete(key) 119: super(convert_key(key)) 120: end
Returns an exact copy of the hash.
[ show source ]
# File activesupport/lib/active_support/hash_with_indifferent_access.rb, line 97 97: def dup 98: HashWithIndifferentAccess.new(self) 99: end
[ show source ]
# File activesupport/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]
[ show source ]
# File activesupport/lib/active_support/hash_with_indifferent_access.rb, line 81 81: def fetch(key, *extras) 82: super(convert_key(key), *extras) 83: 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/hash_with_indifferent_access.rb, line 72 72: def key?(key) 73: super(convert_key(key)) 74: 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/hash_with_indifferent_access.rb, line 103 103: def merge(hash) 104: self.dup.update(hash) 105: 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/hash_with_indifferent_access.rb, line 109 109: def reverse_merge(other_hash) 110: super self.class.new_from_hash_copying_default(other_hash) 111: end
[ show source ]
# File activesupport/lib/active_support/hash_with_indifferent_access.rb, line 113 113: def reverse_merge!(other_hash) 114: replace(reverse_merge( other_hash )) 115: end
[ show source ]
# File activesupport/lib/active_support/hash_with_indifferent_access.rb, line 123 123: def stringify_keys; dup end
[ show source ]
# File activesupport/lib/active_support/hash_with_indifferent_access.rb, line 122 122: def stringify_keys!; self end
[ show source ]
# File activesupport/lib/active_support/hash_with_indifferent_access.rb, line 125 125: def symbolize_keys; to_hash.symbolize_keys end
Convert to a Hash with String keys.
[ show source ]
# File activesupport/lib/active_support/hash_with_indifferent_access.rb, line 129 129: def to_hash 130: Hash.new(default).merge!(self) 131: end
[ show source ]
# File activesupport/lib/active_support/hash_with_indifferent_access.rb, line 126 126: 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/hash_with_indifferent_access.rb, line 58 58: def update(other_hash) 59: other_hash.each_pair { |key, value| regular_writer(convert_key(key), convert_value(value)) } 60: self 61: 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/hash_with_indifferent_access.rb, line 92 92: def values_at(*indices) 93: indices.collect {|key| self[convert_key(key)]} 94: end
[ show source ]
# File activesupport/lib/active_support/hash_with_indifferent_access.rb, line 134 134: def convert_key(key) 135: key.kind_of?(Symbol) ? key.to_s : key 136: end
[ show source ]
# File activesupport/lib/active_support/hash_with_indifferent_access.rb, line 138 138: def convert_value(value) 139: case value 140: when Hash 141: self.class.new_from_hash_copying_default(value) 142: when Array 143: value.collect { |e| e.is_a?(Hash) ? self.class.new_from_hash_copying_default(e) : e } 144: else 145: value 146: end 147: end