Class: DefaultInitHash

Inherits:
Hash show all
Defined in:
lib/rpdf2txt-rockit/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

Methods inherited from Hash

#to_compact_src, #to_src

Methods included from SourceCodeDumpable

as_code, as_method_named, as_module_method_named, #create_new, indent_lines, name_hash, #new_of_my_type, #parameter_named, #to_compact_src, #to_src_in_module, #type_to_src

Constructor Details

#initialize(*args, &initblock) ⇒ DefaultInitHash

Returns a new instance of DefaultInitHash.



43
44
45
46
# File 'lib/rpdf2txt-rockit/base_extensions.rb', line 43

def initialize(*args, &initblock)
  super(*args)
  @initblock = initblock
end

Instance Method Details

#[](key) ⇒ Object



48
49
50
51
52
53
# File 'lib/rpdf2txt-rockit/base_extensions.rb', line 48

def [](key)
#super(key) || (self[key] = @initblock.call(key))
fetch(key) {
	store(key, @initblock.call(key))
}
end