Is it possible to do something like this:
hash[:key] = something
hash[:key] #=> 1
hash[:key] #=> 2
hash[:key] #=> 3
where I store something in a hash that automatically increments each time it is accessed
Apparently it is if you subclass Hash and store lambdas:
class CallHash < Hash
def [](key)
if fetch(key).respond_to?(:call)
fetch(key).call
else
fetch(key)
end
rescue KeyError
nil
end
end
class CallHash < Hash
def [](key)
value = fetch key, nil
return value.call if value.respond_to?(:call)
value
end
end
No any search results
You already invited:
2 Answers
Walter
Upvotes from:
Amos
Upvotes from: