Class: HashWithIndifferentAccess

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

Overview

this class has dubious semantics and we only have it so that people can write params instead of params

Constant Summary

Constants included from ActiveSupport::CoreExtensions::Hash::Conversions

ActiveSupport::CoreExtensions::Hash::Conversions::XML_FORMATTING, ActiveSupport::CoreExtensions::Hash::Conversions::XML_TYPE_NAMES

Instance Method Summary collapse

Methods included from ActiveSupport::CoreExtensions::Hash::Diff

#diff

Methods included from ActiveSupport::CoreExtensions::Hash::Conversions

#to_xml

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.



5
6
7
8
9
10
11
12
# File 'lib/active_support/core_ext/hash/indifferent_access.rb', line 5

def initialize(constructor = {})
  if constructor.is_a?(Hash)
    super()
    update(constructor)
  else
    super(constructor)
  end
end

Instance Method Details

#[]=(key, value) ⇒ Object



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

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

#default(key) ⇒ Object



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

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

#delete(key) ⇒ Object



56
57
58
# File 'lib/active_support/core_ext/hash/indifferent_access.rb', line 56

def delete(key)
  super(convert_key(key))
end

#dupObject



48
49
50
# File 'lib/active_support/core_ext/hash/indifferent_access.rb', line 48

def dup
  HashWithIndifferentAccess.new(self)
end

#fetch(key, *extras) ⇒ Object



40
41
42
# File 'lib/active_support/core_ext/hash/indifferent_access.rb', line 40

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

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

Returns:

  • (Boolean)


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

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

#merge(hash) ⇒ Object



52
53
54
# File 'lib/active_support/core_ext/hash/indifferent_access.rb', line 52

def merge(hash)
  self.dup.update(hash)
end

#regular_updateObject



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

alias_method :regular_update, :update

#regular_writerObject



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

alias_method :regular_writer, :[]=

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



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

def update(other_hash)
  other_hash.each_pair { |key, value| regular_writer(convert_key(key), convert_value(value)) }
  self
end

#values_at(*indices) ⇒ Object



44
45
46
# File 'lib/active_support/core_ext/hash/indifferent_access.rb', line 44

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