Today I Learned

hashrocket A Hashrocket project

Create A Hash From An Array Of Arrays

The ::[] method on the Hash class allows you to succinctly create a hash from an array of arrays -- or rather an array of tuples which are key value pairs.

> Hash[ [["a",2],["b",4]] ]
{"a"=>2, "b"=>4}
> Hash[ [[1,2],[3,4]] ]
{1=>2, 3=>4}

See the Hash::[] docs for more details.

h/t Josh Davey

See More #ruby TILs
Looking for help? Each developer at Hashrocket has years of experience working with Ruby applications of all types and sizes. We're an active presence at Ruby conferences, have written some of the most popular gems, and have worked on many of the web's Ruby on Rails success stories. Contact us today to talk about your Ruby project.