Class: Jikanrb::IndifferentHash
- Inherits:
-
Hash
- Object
- Hash
- Jikanrb::IndifferentHash
- Defined in:
- lib/jikanrb/utils.rb
Overview
A Hash that allows access with both Symbol and String keys. This class provides a recursive mechanism to ensure nested Hashes also behave indifferently, similar to ActiveSupport’s HashWithIndifferentAccess.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Retrieves the value object corresponding to the key object.
-
#[]=(key, value) ⇒ Object
Associates the value given by value with the key given by key.
-
#fetch(key, *args) ⇒ Object
Returns a key’s value, or the default value if the key is not found.
-
#initialize(hash = {}) ⇒ IndifferentHash
constructor
Initializes a new IndifferentHash.
-
#key?(key) ⇒ Boolean
(also: #include?, #has_key?, #member?)
Returns true if the given key is present in the hash.
Constructor Details
#initialize(hash = {}) ⇒ IndifferentHash
Initializes a new IndifferentHash.
11 12 13 14 |
# File 'lib/jikanrb/utils.rb', line 11 def initialize(hash = {}) super() hash.each { |key, value| self[key] = value } end |
Instance Method Details
#[](key) ⇒ Object
Retrieves the value object corresponding to the key object. The key is automatically converted to a string.
21 22 23 |
# File 'lib/jikanrb/utils.rb', line 21 def [](key) super(convert_key(key)) end |
#[]=(key, value) ⇒ Object
Associates the value given by value with the key given by key. The key is automatically converted to a string. The value is processed to ensure nested structures are also indifferent.
32 33 34 |
# File 'lib/jikanrb/utils.rb', line 32 def []=(key, value) super(convert_key(key), convert_value(value)) end |
#fetch(key, *args) ⇒ Object
Returns a key’s value, or the default value if the key is not found. The key is automatically converted to a string.
42 43 44 |
# File 'lib/jikanrb/utils.rb', line 42 def fetch(key, *args) super(convert_key(key), *args) end |
#key?(key) ⇒ Boolean Also known as: include?, has_key?, member?
Returns true if the given key is present in the hash. The key is automatically converted to a string.
51 52 53 |
# File 'lib/jikanrb/utils.rb', line 51 def key?(key) super(convert_key(key)) end |