Ruby we might Miss

This is the continuation of the series I started.
The topic for this post :

• accepts a block, which provides a way to calculate a default value if the hash
has none. This is useful for caching. The first time a cached method is called with a
particular set of arguments, the block is invoked; it calculates the value and stores it
in the hash for future access. ActiveSupport has an implementation of hash caching in
caching_tools.rb, which generates hashes like this: do |as, a|
as[a] = do |bs, b|
bs[b] = slow_method(a, b)

Hash#delete removes a value from the hash and returns it. This is useful for stripping
out keyword arguments from a hash before passing it along somewhere.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s