Module: Nuggets::Hash::SeenMixin

Included in:
Hash
Defined in:
lib/nuggets/hash/seen_mixin.rb

Instance Method Summary collapse

Instance Method Details

#seen(seen = true, unseen = false) ⇒ Object

call-seq:

Hash.seen([seen[, unseen]]) => aHash

Returns a hash that returns unseen as the default value for a key that wasn’t seen before and seen for a key that was.

Examples:

hash = Hash.seen
hash[:foo]  #=> false
hash[:foo]  #=> true
hash[:foo]  #=> true
hash[:bar]  #=> false
hash[:bar]  #=> true

hash = Hash.seen(42, 23)
hash[:foo]  #=> 23
hash[:foo]  #=> 42
hash[:foo]  #=> 42
hash[:bar]  #=> 23
hash[:bar]  #=> 42


52
53
54
# File 'lib/nuggets/hash/seen_mixin.rb', line 52

def seen(seen = true, unseen = false)
  new { |hash, key| hash[key] = seen; unseen }
end