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
Post a Comment