Menu

Toshimaru's Blog

Array to Hash in Ruby

You can convert from Array to Hash in Ruby using Array#to_h.

["a", "b", "c"].to_h { |str| [str, str] }
# => {"a"=>"a", "b"=>"b", "c"=>"c"}
[1, 2, 3].to_h { |num| [num, num * num] }
# => {1=>1, 2=>4, 3=>9}
Load more