| Class | HashWithIndifferentAccess |
| In: |
vendor/rails/activesupport/lib/active_support/core_ext/hash/indifferent_access.rb
|
| Parent: | Hash |
This class has dubious semantics and we only have it so that people can write params[:key] instead of params[‘key’]
| []= | -> | regular_writer |
# File vendor/rails/activesupport/lib/active_support/core_ext/hash/indifferent_access.rb, line 5
5: def initialize(constructor = {})
6: if constructor.is_a?(Hash)
7: super()
8: update(constructor)
9: else
10: super(constructor)
11: end
12: end
# File vendor/rails/activesupport/lib/active_support/core_ext/hash/indifferent_access.rb, line 25
25: def []=(key, value)
26: regular_writer(convert_key(key), convert_value(value))
27: end
# File vendor/rails/activesupport/lib/active_support/core_ext/hash/indifferent_access.rb, line 74
74: def convert_key(key)
75: key.kind_of?(Symbol) ? key.to_s : key
76: end
# File vendor/rails/activesupport/lib/active_support/core_ext/hash/indifferent_access.rb, line 78
78: def convert_value(value)
79: case value
80: when Hash
81: value.with_indifferent_access
82: when Array
83: value.collect { |e| e.is_a?(Hash) ? e.with_indifferent_access : e }
84: else
85: value
86: end
87: end
# File vendor/rails/activesupport/lib/active_support/core_ext/hash/indifferent_access.rb, line 14
14: def default(key = nil)
15: if key.is_a?(Symbol) && include?(key = key.to_s)
16: self[key]
17: else
18: super
19: end
20: end
# File vendor/rails/activesupport/lib/active_support/core_ext/hash/indifferent_access.rb, line 60
60: def delete(key)
61: super(convert_key(key))
62: end
# File vendor/rails/activesupport/lib/active_support/core_ext/hash/indifferent_access.rb, line 52
52: def dup
53: HashWithIndifferentAccess.new(self)
54: end
# File vendor/rails/activesupport/lib/active_support/core_ext/hash/indifferent_access.rb, line 44
44: def fetch(key, *extras)
45: super(convert_key(key), *extras)
46: end
# File vendor/rails/activesupport/lib/active_support/core_ext/hash/indifferent_access.rb, line 36
36: def key?(key)
37: super(convert_key(key))
38: end
# File vendor/rails/activesupport/lib/active_support/core_ext/hash/indifferent_access.rb, line 56
56: def merge(hash)
57: self.dup.update(hash)
58: end
# File vendor/rails/activesupport/lib/active_support/core_ext/hash/indifferent_access.rb, line 64
64: def stringify_keys!; self end
# File vendor/rails/activesupport/lib/active_support/core_ext/hash/indifferent_access.rb, line 65
65: def symbolize_keys!; self end
Convert to a Hash with String keys.
# File vendor/rails/activesupport/lib/active_support/core_ext/hash/indifferent_access.rb, line 69
69: def to_hash
70: Hash.new(default).merge(self)
71: end
# File vendor/rails/activesupport/lib/active_support/core_ext/hash/indifferent_access.rb, line 66
66: def to_options!; self end
# File vendor/rails/activesupport/lib/active_support/core_ext/hash/indifferent_access.rb, line 29
29: def update(other_hash)
30: other_hash.each_pair { |key, value| regular_writer(convert_key(key), convert_value(value)) }
31: self
32: end