ruby - Converting nested array to flat hash -


i have this:

[["hello", 1], ["world", 1]]  

i want this:

{ "hello" => 1, "world" => 1 } 

i've coded works, feels stupid. here is:

hash = {} array.each |element|   hash[element[0]] = element[1] end  hash 

is there better way?

yes.. below using hash[ [ [key, value], ... ] ] → new_hash

hash[[["hello", 1], ["world", 1]]] # =>  => {"hello"=>1, "world"=>1}  

if in ruby2.1 use array#to_h

[["hello", 1], ["world", 1]].to_h 

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -