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::[]