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
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 118
118:     def delete(key)
119:       super(convert_key(key))
120:     end
dup() click to toggle source

Returns an exact copy of the hash.

    # File lib/active_support/hash_with_indifferent_access.rb, line 97
97:     def dup
98:       HashWithIndifferentAccess.new(self)
99:     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 81
81:     def fetch(key, *extras)
82:       super(convert_key(key), *extras)
83:     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 72
72:     def key?(key)
73:       super(convert_key(key))
74:     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 103
103:     def merge(hash)
104:       self.dup.update(hash)
105:     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 109
109:     def reverse_merge(other_hash)
110:       super self.class.new_from_hash_copying_default(other_hash)
111:     end
reverse_merge!(other_hash) click to toggle source
     # File lib/active_support/hash_with_indifferent_access.rb, line 113
113:     def reverse_merge!(other_hash)
114:       replace(reverse_merge( other_hash ))
115:     end
stringify_keys() click to toggle source
     # File lib/active_support/hash_with_indifferent_access.rb, line 123
123:     def stringify_keys; dup end
stringify_keys!() click to toggle source
     # File lib/active_support/hash_with_indifferent_access.rb, line 122
122:     def stringify_keys!; self end
symbolize_keys() click to toggle source
     # File lib/active_support/hash_with_indifferent_access.rb, line 125
125:     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 129
129:     def to_hash
130:       Hash.new(default).merge!(self)
131:     end
to_options!() click to toggle source
     # File lib/active_support/hash_with_indifferent_access.rb, line 126
126:     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 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
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 92
92:     def values_at(*indices)
93:       indices.collect {|key| self[convert_key(key)]}
94:     end

Protected Instance Methods

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

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.