Class | SavingHash |
In: |
lib/sup/util.rb
|
Parent: | Object |
acts like a hash with an initialization block, but saves any newly-created value even upon lookup.
for example:
class C
attr_accessor :val def initialize; @val = 0 end
end
h = Hash.new { C.new } h[:a].val # => 0 h[:a].val = 1 h[:a].val # => 0
h2 = SavingHash.new { C.new } h2[:a].val # => 0 h2[:a].val = 1 h2[:a].val # => 1
important note: you REALLY want to use member? to test existence, because just checking h[anything] will always evaluate to true (except for degenerate constructor blocks that return nil or false)