假设我有这两个数组:
a = [1, 2, 3, 4, 5]
b = [6, 7, 8, 9, 10]
我想要得到的是一个哈希,如下所示:
c = { 1 => [1, 6], 2 => [2, 7], 3 => [3, 8], 4 => [4, 9], 5 => [5, 10] }
到目前为止,我遇到的唯一方法如下:
# Initialize the resulting Hash and fill in the keys.
c = {}
(a.length).times { |i| c[i + 1] = [] }
# Fill values
c.each_with_index do |(key), idx|
c[key] = [a[idx], b[idx]]
end
Ruby 有更好或更漂亮的方法来做到这一点吗?
提前致谢。