Ruby’s implementation of to_h has finally been added to the core Array class. This method converts our multidimensional array into a key-value store.

>> [[1,2]].to_h
=> {1=>2}
 
>> [[1,2], [:party, :fun_times]].to_h
=> {1=>2, :party=>:fun_times}
 
>> [1,2].to_h
TypeError: wrong element type Fixnum at 0 (expected array)

:)

Leave a Reply

Your email address will not be published. Required fields are marked *