Class: DefaultInitHash
- Inherits:
-
Hash
- Object
- Hash
- DefaultInitHash
- Defined in:
- lib/graphr/base_extensions.rb
Overview
Hash which takes a block that is called to give a default value when a key has the value nil in the hash.
Direct Known Subclasses
Instance Method Summary collapse
- #[](key) ⇒ Object
-
#initialize(*args, &initblock) ⇒ DefaultInitHash
constructor
A new instance of DefaultInitHash.
Constructor Details
#initialize(*args, &initblock) ⇒ DefaultInitHash
Returns a new instance of DefaultInitHash.
43 44 45 46 |
# File 'lib/graphr/base_extensions.rb', line 43 def initialize(*args, &initblock) super(*args) @initblock = initblock end |
Instance Method Details
#[](key) ⇒ Object
48 49 50 |
# File 'lib/graphr/base_extensions.rb', line 48 def [](key) super(key) || (self[key] = @initblock.call(key)) end |