Create An Array With A Single Hash Without The Curly Braces In Ruby

Recently, I forgot to include curly braces for my array containing a hash.

Instead of this:

[{ name: 'Taylor', age: 36 }]

You can do this:

[name: 'Taylor', age: 36]

The result for both of those will be:

[{:name=>"Taylor", :age=>36}]

This falls under the “at first it seems immoral to do this, but the more I do it, the more I feel this is how the world should be” category.

I find this approach most useful in tests: what I’m testing takes an array of hashes, and for testing purposes I only need a single hash in the array.

Enjoy!