Ever have to map all the keys of a Hash, or Hash-like Ruby object? For example, you have a Hash mapping keys to the binary contents of several files, and you want to present those file contents as Base64.

Imagine we have a Hash that looks something like this:

span class="st0">'goblins''kittens'

If you were just doing this mapping inline, it might look like this:

 

Instead of typing out almost-identical code over and over again, here’s an implementation as a simple block-receiving method on Hash itself.

 

Now the code to Base64 encode the file contents becomes this much simpler snippet.

 

Leave a Comment

Enclose code in <code lang="ruby"></code> if you care.
Preview your comment using the button below.