Recursively Symbolize Keys

Given a YAML stream that looks like this:

The Ruby-fied version looks like this:

But we don’t like string keys, we like symbol keys. A number of libraries exist to extend Hash with interchangeable string- or key-based indexing. But adding a library dependency seems like overkill for symbolizing some keys. Here’s a quick recursive string-to-symbol key converter:

Note the block parameters to Hash#inject.

The block arguments would normally be a hash (the accumulator) and a two-element array of [key, value]. Putting parentheses around the second two parameters causes the second argument to be “splatted” into its component parts and assigned to key and value separately.

Let’s take a look at the output.