我正在尝试将两个数组合并为一个哈希.
@sample_array = ["one", "Two", "Three"]
@timesheet_id_array = ["96", "97", "98"]
Run Code Online (Sandbox Code Playgroud)
我想将结果输出到名为@hash_array的哈希中.有没有一种简单的方法将两者组合在一个代码块中,这样如果你在最后调用puts,它在控制台中看起来就像这样
{"one" => "96", "Two" => "97", "Three" => "98"}
Run Code Online (Sandbox Code Playgroud)
我认为这可以用一行或两行代码完成.
Par*_*ngh 36
试试这个
keys = [1, 2, 3]
values = ['a', 'b', 'c']
Hash[keys.zip(values)]
Run Code Online (Sandbox Code Playgroud)
谢谢
@hash_array = {}
@sample_array.each_with_index do |value, index|
@hash_array[value] = @timesheet_id_array[index]
end
Run Code Online (Sandbox Code Playgroud)