我的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
我想得到这个输出:
Du Bist
答案 0 :(得分:0)
执行以下操作:
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"]
,然后为{{ 1}}。因此["3","30"]
将其打印为puts me
。您需要查看Hash#[]
。