Class: HashWithIndifferentAccess

Inherits:
Hash show all
Defined in:
lib/active_support/core_ext/hash/indifferent_access.rb

Overview

This implementation is HODEL-HASH-9600 compliant

Instance Method Summary collapse

Methods included from ActiveSupport::CoreExtensions::Hash::ReverseMerge

#reverse_merge, #reverse_merge!

Methods included from ActiveSupport::CoreExtensions::Hash::IndifferentAccess

#with_indifferent_access

Methods included from ActiveSupport::CoreExtensions::Hash::Keys

#assert_valid_keys, #stringify_keys, #stringify_keys!, #symbolize_keys, #symbolize_keys!

Constructor Details

#initialize(constructor = {}) ⇒ HashWithIndifferentAccess

Returns a new instance of HashWithIndifferentAccess.



3
4
5
6
7
8
9
10
# File 'lib/active_support/core_ext/hash/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



18
19
20
# File 'lib/active_support/core_ext/hash/indifferent_access.rb', line 18

def []=(key, value)
  regular_writer(convert_key(key), convert_value(value))
end

#default(key) ⇒ Object



12
13
14
# File 'lib/active_support/core_ext/hash/indifferent_access.rb', line 12

def default(key)
  self[key.to_s] if key.is_a?(Symbol)
end

#fetch(key, *extras) ⇒ Object



33
34
35
# File 'lib/active_support/core_ext/hash/indifferent_access.rb', line 33

def fetch(key, *extras)
  super(convert_key(key), *extras)
end

#key?(key) ⇒ Boolean Also known as: include?, has_key?, member?

Returns:

  • (Boolean)


25
26
27
# File 'lib/active_support/core_ext/hash/indifferent_access.rb', line 25

def key?(key)
  super(convert_key(key))
end

#regular_writerObject



16
# File 'lib/active_support/core_ext/hash/indifferent_access.rb', line 16

alias_method :regular_writer, :[]=

#update(hash) ⇒ Object



21
22
23
# File 'lib/active_support/core_ext/hash/indifferent_access.rb', line 21

def update(hash)
  hash.each {|key, value| self[key] = value}
end

#values_at(*indices) ⇒ Object



37
38
39
# File 'lib/active_support/core_ext/hash/indifferent_access.rb', line 37

def values_at(*indices)
  indices.collect {|key| self[convert_key(key)]}
end