我的Ruby代码中有2个哈希。
我想在"c"循环中获取"d"哈希的数据。
c = {"2"=>"20", "3"=>"30"}
d = {"2"=>"Du", "3"=>"Bist"}
c.each_with_index do |me,index|
puts me
end
输出为:
2 20 3 30
我想获得此输出:
杜比斯特
按以下步骤操作:
c = {"2"=>"20", "3"=>"30"}
d = {"2"=>"Du", "3"=>"Bist"}
c.each_with_index do |(k,v),i|
puts "#{d[k]} at index #{i}"
end
# >> Du at index 0
# >> Bist at index 1
# I don't know why you want each_with_index, instead of each here
# But here is how you can do.
c.each_with_index do |(k,v),i|
puts d[k]
end
# >> Du
# >> Bist
c
和d
Hash
.c.each_with_index do |me,index|
这里,每次迭代me
首先具有 ["2","20"]
的值,然后是 ["3","30"]
。因此puts me
将其打印为2 20 3 30
。你需要看看Hash#[]
.