Class: HashWithIndifferentAccess

Inherits:
Hash
  • Object
show all
Defined in:
lib/openhash/hash_with_indifferent_access.rb

Overview

Copied from Rails 2.2 stable

Direct Known Subclasses

OpenHash

Instance Method Summary collapse

Methods inherited from Hash

#to_openhash

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

#dupObject

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

Returns:

  • (Boolean)


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_updateObject



21
# File 'lib/openhash/hash_with_indifferent_access.rb', line 21

alias_method :regular_update, :update

#regular_writerObject



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_hashObject

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 to_options!; self end

#update(other_hash) ⇒ Object Also known as: merge!

Updates the instantized hash with values from the second:

hash_1 = HashWithIndifferentAccess.new hash_1 = “value”

hash_2 = HashWithIndifferentAccess.new hash_2 = “New Value!”

hash_1.update(hash_2) # => Value!”



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

Returns an array of the values at the specified indices:

hash = HashWithIndifferentAccess.new hash = “x” hash = “y” hash.values_at(“a”, “b”) # => [“x”, “y”]



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