如何从keys数组和values数组创建哈希?

时间:2016-05-25 09:35:02

标签: ruby hashmap

我从HTML页面中提取了两个数组:

@row_left = ['Title:', 'Author:', 'Price:', 'Description:', 'Seller:']    
@row_right = ['The Well-Grounded Rubyist', 'David A. Black', '$34.99', 'A great book for Rubyists', 'Ruby Scholar']

如何将两个数组合并为一个哈希?

{
  "Title:" => "The Well-Grounded Rubyist",
  "Author:" => "David A. Black",
  "Price:" => "$34.99",
  "Description:" => "A great book for Rubyists",
  "Seller:" => "Ruby Scholar"
}

1 个答案:

答案 0 :(得分:3)

如果您正在寻找将两个相等长度的数组合并到一个哈希中,我会这样做:

a = [1,3,5]; b=[2,4,6]
Hash[a.zip(b)]