Parent

Class Index [+]

Quicksearch

Mail::IndifferentHash

Public Class Methods

new(constructor = {}) click to toggle source
    # File lib/mail/indifferent_hash.rb, line 9
 9:     def initialize(constructor = {})
10:       if constructor.is_a?(Hash)
11:         super()
12:         update(constructor)
13:       else
14:         super(constructor)
15:       end
16:     end
new_from_hash_copying_default(hash) click to toggle source
    # File lib/mail/indifferent_hash.rb, line 26
26:     def self.new_from_hash_copying_default(hash)
27:       IndifferentHash.new(hash).tap do |new_hash|
28:         new_hash.default = hash.default
29:       end
30:     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/mail/indifferent_hash.rb, line 40
40:     def []=(key, value)
41:       regular_writer(convert_key(key), convert_value(value))
42:     end
Also aliased as: regular_writer, store
default(key = nil) click to toggle source
    # File lib/mail/indifferent_hash.rb, line 18
18:     def default(key = nil)
19:       if key.is_a?(Symbol) && include?(key = key.to_s)
20:         self[key]
21:       else
22:         super
23:       end
24:     end
delete(key) click to toggle source

Removes a specified key from the hash.

     # File lib/mail/indifferent_hash.rb, line 116
116:     def delete(key)
117:       super(convert_key(key))
118:     end
dup() click to toggle source

Returns an exact copy of the hash.

    # File lib/mail/indifferent_hash.rb, line 95
95:     def dup
96:       IndifferentHash.new(self)
97:     end
fetch(key, *extras) click to toggle source

Fetches the value for the specified key, same as doing hash[key]

    # File lib/mail/indifferent_hash.rb, line 79
79:     def fetch(key, *extras)
80:       super(convert_key(key), *extras)
81:     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/mail/indifferent_hash.rb, line 70
70:     def key?(key)
71:       super(convert_key(key))
72:     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/mail/indifferent_hash.rb, line 101
101:     def merge(hash)
102:       self.dup.update(hash)
103:     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/mail/indifferent_hash.rb, line 107
107:     def reverse_merge(other_hash)
108:       super self.class.new_from_hash_copying_default(other_hash)
109:     end
reverse_merge!(other_hash) click to toggle source
     # File lib/mail/indifferent_hash.rb, line 111
111:     def reverse_merge!(other_hash)
112:       replace(reverse_merge( other_hash ))
113:     end
store(key, value) click to toggle source
Alias for: []=
stringify_keys() click to toggle source
     # File lib/mail/indifferent_hash.rb, line 121
121:     def stringify_keys; dup end
stringify_keys!() click to toggle source
     # File lib/mail/indifferent_hash.rb, line 120
120:     def stringify_keys!; self end
symbolize_keys() click to toggle source
     # File lib/mail/indifferent_hash.rb, line 122
122:     def symbolize_keys; to_hash.symbolize_keys end
to_hash() click to toggle source
     # File lib/mail/indifferent_hash.rb, line 125
125:     def to_hash
126:       Hash.new(default).merge!(self)
127:     end
to_options!() click to toggle source
     # File lib/mail/indifferent_hash.rb, line 123
123:     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/mail/indifferent_hash.rb, line 56
56:     def update(other_hash)
57:       other_hash.each_pair { |key, value| regular_writer(convert_key(key), convert_value(value)) }
58:       self
59:     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/mail/indifferent_hash.rb, line 90
90:     def values_at(*indices)
91:       indices.collect {|key| self[convert_key(key)]}
92:     end

Protected Instance Methods

convert_key(key) click to toggle source
     # File lib/mail/indifferent_hash.rb, line 131
131:     def convert_key(key)
132:       key.kind_of?(Symbol) ? key.to_s : key
133:     end
convert_value(value) click to toggle source
     # File lib/mail/indifferent_hash.rb, line 135
135:     def convert_value(value)
136:       if value.class == Hash
137:         self.class.new_from_hash_copying_default(value)
138:       elsif value.is_a?(Array)
139:         value.dup.replace(value.map { |e| convert_value(e) })
140:       else
141:         value
142:       end
143:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.