Class: DefaultInitHash

Inherits:
Hash
  • Object
show all
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

HashOfHash

Instance Method Summary collapse

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