Class: Luoma::ChainHash
- Inherits:
-
Object
- Object
- Luoma::ChainHash
- Defined in:
- lib/luoma/chain_hash.rb,
sig/luoma/chain_hash.rbs
Overview
Combine multiple hashes for sequential lookup.
Instance Method Summary collapse
-
#[](key) ⇒ Object
(String) -> untyped.
-
#fetch(key, default = :nothing) ⇒ Object
(String, untyped) -> untyped.
-
#initialize(*hashes) ⇒ ChainHash
constructor
(*_Namespace) -> void.
-
#key?(key) ⇒ Object
(String) -> bool.
- #nil? ⇒ Boolean
- #pop ⇒ _Namespace?
- #push(hash) ⇒ void (also: #<<)
- #size ⇒ Integer
Constructor Details
#initialize(*hashes) ⇒ ChainHash
(*_Namespace) -> void
7 8 9 |
# File 'lib/luoma/chain_hash.rb', line 7 def initialize(*hashes) @hashes = hashes.to_a end |
Instance Method Details
#[](key) ⇒ Object
(String) -> untyped
12 13 14 15 16 17 18 19 20 |
# File 'lib/luoma/chain_hash.rb', line 12 def [](key) index = @hashes.length - 1 while index >= 0 h = @hashes[index] index -= 1 v = h.fetch(key, :nothing) return v unless v == :nothing end end |
#fetch(key, default = :nothing) ⇒ Object
(String, untyped) -> untyped
36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/luoma/chain_hash.rb', line 36 def fetch(key, default = :nothing) index = @hashes.length - 1 while index >= 0 h = @hashes[index] index -= 1 v = h.fetch(key, :nothing) return v unless v == :nothing end default end |
#key?(key) ⇒ Object
(String) -> bool
23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/luoma/chain_hash.rb', line 23 def key?(key) index = @hashes.length - 1 while index >= 0 h = @hashes[index] index -= 1 v = h.fetch(key, :nothing) return true unless v == :nothing end false end |
#nil? ⇒ Boolean
52 |
# File 'lib/luoma/chain_hash.rb', line 52 def nil? = false |
#push(hash) ⇒ void Also known as: <<
This method returns an undefined value.
49 |
# File 'lib/luoma/chain_hash.rb', line 49 def push(hash) = @hashes << hash |
#size ⇒ Integer
48 |
# File 'lib/luoma/chain_hash.rb', line 48 def size = @hashes.length |