Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
配列が[1,2,3]あり、そこからハッシュを作成したいので、結果は{"kangaroo"=>1, "moose"=>2, "mouse"=>3}. それを行う最良の方法は何ですか?
[1,2,3]
{"kangaroo"=>1, "moose"=>2, "mouse"=>3}
どうですか:
Hash[%w(kangaroo moose mouse).zip [1,2,3]] # => {"kangaroo"=>1, "moose"=>2, "mouse"=>3}
Array#zip両方の配列を要素ごとに結合します。
Array#zip
%w(kangaroo moose mouse).zip [1,2,3] # => [["kangaroo", 1], ["moose", 2], ["mouse", 3]]
Hash::[]この配列からハッシュを作成します。
Hash::[]