Parent

Class Index [+]

Quicksearch

ActiveSupport::HashWithIndifferentAccess

Public Class Methods

new(constructor = {}) click to toggle source
    # File lib/active_support/hash_with_indifferent_access.rb, line 23
23:     def initialize(constructor = {})
24:       if constructor.is_a?(Hash)
25:         super()
26:         update(constructor)
27:       else
28:         super(constructor)
29:       end
30:     end
new_from_hash_copying_default(hash) click to toggle source
    # File lib/active_support/hash_with_indifferent_access.rb, line 40
40:     def self.new_from_hash_copying_default(hash)
41:       new(hash).tap do |new_hash|
42:         new_hash.default = hash.default
43:       end
44:     end

Public Instance Methods

[]=(key, value) click to toggle source

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
Also aliased as: regular_writer, store
default(key = nil) click to toggle source
    # 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
delete(key) click to toggle source

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
dup() click to toggle source

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
extractable_options?() click to toggle source

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
fetch(key, *extras) click to toggle source

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
has_key?(key) click to toggle source
Alias for: key?
include?(key) click to toggle source
Alias for: key?
key?(key) click to toggle source

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
Also aliased as: include?, has_key?, member?
member?(key) click to toggle source
Alias for: key?
merge(hash) click to toggle source

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
merge!(other_hash) click to toggle source
Alias for: update
nested_under_indifferent_access() click to toggle source
    # File lib/active_support/hash_with_indifferent_access.rb, line 19
19:     def nested_under_indifferent_access
20:       self
21:     end
regular_update(other_hash) click to toggle source
Alias for: update
regular_writer(key, value) click to toggle source
Alias for: []=
reverse_merge(other_hash) click to toggle source

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
reverse_merge!(other_hash) click to toggle source
     # 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
store(key, value) click to toggle source
Alias for: []=
stringify_keys() click to toggle source
     # File lib/active_support/hash_with_indifferent_access.rb, line 151
151:     def stringify_keys; dup end
stringify_keys!() click to toggle source
     # File lib/active_support/hash_with_indifferent_access.rb, line 150
150:     def stringify_keys!; self end
symbolize_keys() click to toggle source
     # File lib/active_support/hash_with_indifferent_access.rb, line 153
153:     def symbolize_keys; to_hash.symbolize_keys end
to_hash() click to toggle source

Convert to a Hash with String keys.

     # File lib/active_support/hash_with_indifferent_access.rb, line 157
157:     def to_hash
158:       Hash.new(default).merge!(self)
159:     end
to_options!() click to toggle source
     # File lib/active_support/hash_with_indifferent_access.rb, line 154
154:     def to_options!; self end
update(other_hash) click to toggle source

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
Also aliased as: regular_update, merge!
values_at(*indices) click to toggle source

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
with_indifferent_access() click to toggle source
    # File lib/active_support/hash_with_indifferent_access.rb, line 15
15:     def with_indifferent_access
16:       dup
17:     end

Protected Instance Methods

convert_key(key) click to toggle source
     # 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
convert_value(value) click to toggle source
     # 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.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.