Class: HashWithIndifferentAccess
- Defined in:
- lib/openhash/hash_with_indifferent_access.rb
Overview
Copied from Rails 2.2 stable
Direct Known Subclasses
Instance Method Summary collapse
-
#[]=(key, value) ⇒ Object
Assigns a new value to the hash:.
- #default(key = nil) ⇒ Object
-
#delete(key) ⇒ Object
Removes a specified key from the hash.
-
#dup ⇒ Object
Returns an exact copy of the hash.
-
#fetch(key, *extras) ⇒ Object
Fetches the value for the specified key, same as doing hash.
-
#initialize(constructor = {}) ⇒ HashWithIndifferentAccess
constructor
A new instance of HashWithIndifferentAccess.
-
#key?(key) ⇒ Boolean
(also: #include?, #has_key?, #member?)
Checks the hash for a key matching the argument passed in:.
-
#merge(hash) ⇒ Object
Merges the instantized and the specified hashes together, giving precedence to the values from the second hash Does not overwrite the existing hash.
- #regular_update ⇒ Object
- #regular_writer ⇒ Object
- #stringify_keys! ⇒ Object
- #symbolize_keys! ⇒ Object
-
#to_hash ⇒ Object
Convert to a Hash with String keys.
- #to_options! ⇒ Object
-
#update(other_hash) ⇒ Object
(also: #merge!)
Updates the instantized hash with values from the second:.
-
#values_at(*indices) ⇒ Object
Returns an array of the values at the specified indices:.
Methods inherited from Hash
Constructor Details
#initialize(constructor = {}) ⇒ HashWithIndifferentAccess
Returns a new instance of HashWithIndifferentAccess.
3 4 5 6 7 8 9 10 |
# File 'lib/openhash/hash_with_indifferent_access.rb', line 3 def initialize(constructor = {}) if constructor.is_a?(Hash) super() update(constructor) else super(constructor) end end |
Instance Method Details
#[]=(key, value) ⇒ Object
Assigns a new value to the hash:
hash = HashWithIndifferentAccess.new hash = “value”
28 29 30 |
# File 'lib/openhash/hash_with_indifferent_access.rb', line 28 def []=(key, value) regular_writer(convert_key(key), convert_value(value)) end |
#default(key = nil) ⇒ Object
12 13 14 15 16 17 18 |
# File 'lib/openhash/hash_with_indifferent_access.rb', line 12 def default(key = nil) if key.is_a?(Symbol) && include?(key = key.to_s) self[key] else super end end |
#delete(key) ⇒ Object
Removes a specified key from the hash.
92 93 94 |
# File 'lib/openhash/hash_with_indifferent_access.rb', line 92 def delete(key) super(convert_key(key)) end |
#dup ⇒ Object
Returns an exact copy of the hash.
81 82 83 |
# File 'lib/openhash/hash_with_indifferent_access.rb', line 81 def dup HashWithIndifferentAccess.new(self) end |
#fetch(key, *extras) ⇒ Object
Fetches the value for the specified key, same as doing hash
65 66 67 |
# File 'lib/openhash/hash_with_indifferent_access.rb', line 65 def fetch(key, *extras) super(convert_key(key), *extras) end |
#key?(key) ⇒ Boolean Also known as: include?, has_key?, member?
Checks the hash for a key matching the argument passed in:
hash = HashWithIndifferentAccess.new hash = “value” hash.key? :key # => true hash.key? “key” # => true
56 57 58 |
# File 'lib/openhash/hash_with_indifferent_access.rb', line 56 def key?(key) super(convert_key(key)) end |
#merge(hash) ⇒ Object
Merges the instantized and the specified hashes together, giving precedence to the values from the second hash Does not overwrite the existing hash.
87 88 89 |
# File 'lib/openhash/hash_with_indifferent_access.rb', line 87 def merge(hash) self.dup.update(hash) end |
#regular_update ⇒ Object
21 |
# File 'lib/openhash/hash_with_indifferent_access.rb', line 21 alias_method :regular_update, :update |
#regular_writer ⇒ Object
20 |
# File 'lib/openhash/hash_with_indifferent_access.rb', line 20 alias_method :regular_writer, :[]= |
#stringify_keys! ⇒ Object
96 |
# File 'lib/openhash/hash_with_indifferent_access.rb', line 96 def stringify_keys!; self end |
#symbolize_keys! ⇒ Object
97 |
# File 'lib/openhash/hash_with_indifferent_access.rb', line 97 def symbolize_keys!; self end |
#to_hash ⇒ Object
Convert to a Hash with String keys.
101 102 103 |
# File 'lib/openhash/hash_with_indifferent_access.rb', line 101 def to_hash Hash.new(default).merge(self) end |
#to_options! ⇒ Object
98 |
# File 'lib/openhash/hash_with_indifferent_access.rb', line 98 def ; self end |
#update(other_hash) ⇒ Object Also known as: merge!
42 43 44 45 |
# File 'lib/openhash/hash_with_indifferent_access.rb', line 42 def update(other_hash) other_hash.each_pair { |key, value| regular_writer(convert_key(key), convert_value(value)) } self end |
#values_at(*indices) ⇒ Object
76 77 78 |
# File 'lib/openhash/hash_with_indifferent_access.rb', line 76 def values_at(*indices) indices.collect {|key| self[convert_key(key)]} end |