Class: Vli::Util::HashWithIndifferentAccess
- Inherits:
-
Hash
- Object
- Hash
- Vli::Util::HashWithIndifferentAccess
- Defined in:
- lib/vli/util/hash_with_indifferent_access.rb
Overview
A hash with indifferent access. Mostly taken from Thor/Rails (thanks). Normally I’m not a fan of using an indifferent access hash since Symbols are basically memory leaks in Ruby, but since Vli is typically a quick one-off binary run and it doesn’t use too many hash keys where this is used, the effect should be minimal.
hash[:foo] #=> 'bar'
hash['foo'] #=> 'bar'
Direct Known Subclasses
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #delete(key) ⇒ Object
-
#initialize(hash = {}, &block) ⇒ HashWithIndifferentAccess
constructor
A new instance of HashWithIndifferentAccess.
- #key?(key) ⇒ Boolean (also: #include?, #has_key?, #member?)
- #merge(other) ⇒ Object
- #merge!(other) ⇒ Object
- #values_at(*indices) ⇒ Object
Constructor Details
#initialize(hash = {}, &block) ⇒ HashWithIndifferentAccess
Returns a new instance of HashWithIndifferentAccess.
13 14 15 16 17 18 19 |
# File 'lib/vli/util/hash_with_indifferent_access.rb', line 13 def initialize(hash={}, &block) super(&block) hash.each do |key, value| self[convert_key(key)] = value end end |
Instance Method Details
#[](key) ⇒ Object
21 22 23 |
# File 'lib/vli/util/hash_with_indifferent_access.rb', line 21 def [](key) super(convert_key(key)) end |
#[]=(key, value) ⇒ Object
25 26 27 |
# File 'lib/vli/util/hash_with_indifferent_access.rb', line 25 def []=(key, value) super(convert_key(key), value) end |
#delete(key) ⇒ Object
29 30 31 |
# File 'lib/vli/util/hash_with_indifferent_access.rb', line 29 def delete(key) super(convert_key(key)) end |
#key?(key) ⇒ Boolean Also known as: include?, has_key?, member?
48 49 50 |
# File 'lib/vli/util/hash_with_indifferent_access.rb', line 48 def key?(key) super(convert_key(key)) end |
#merge(other) ⇒ Object
37 38 39 |
# File 'lib/vli/util/hash_with_indifferent_access.rb', line 37 def merge(other) dup.merge!(other) end |
#merge!(other) ⇒ Object
41 42 43 44 45 46 |
# File 'lib/vli/util/hash_with_indifferent_access.rb', line 41 def merge!(other) other.each do |key, value| self[convert_key(key)] = value end self end |
#values_at(*indices) ⇒ Object
33 34 35 |
# File 'lib/vli/util/hash_with_indifferent_access.rb', line 33 def values_at(*indices) indices.collect { |key| self[convert_key(key)] } end |