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 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
new_from_hash_copying_default(hash) click to toggle source
    # File 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

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 44
44:     def []=(key, value)
45:       regular_writer(convert_key(key), convert_value(value))
46:     end
Also aliased as: regular_writer, store
default(key = nil) click to toggle source
    # 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
delete(key) click to toggle source

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

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
extractable_options?() click to toggle source
    # File lib/active_support/hash_with_indifferent_access.rb, line 9
 9:     def extractable_options?
10:       true
11:     end
fetch(key, *extras) click to toggle source

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
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 74
74:     def key?(key)
75:       super(convert_key(key))
76:     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 105
105:     def merge(hash)
106:       self.dup.update(hash)
107:     end
merge!(other_hash) click to toggle source
Alias for: update
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 111
111:     def reverse_merge(other_hash)
112:       super self.class.new_from_hash_copying_default(other_hash)
113:     end
reverse_merge!(other_hash) click to toggle source
     # 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
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 125
125:     def stringify_keys; dup end
stringify_keys!() click to toggle source
     # File lib/active_support/hash_with_indifferent_access.rb, line 124
124:     def stringify_keys!; self end
symbolize_keys() click to toggle source
     # File lib/active_support/hash_with_indifferent_access.rb, line 127
127:     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 131
131:     def to_hash
132:       Hash.new(default).merge!(self)
133:     end
to_options!() click to toggle source
     # File lib/active_support/hash_with_indifferent_access.rb, line 128
128:     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 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
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 94
94:     def values_at(*indices)
95:       indices.collect {|key| self[convert_key(key)]}
96:     end

Protected Instance Methods

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

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.