如何在其他哈希循环中抓取其他哈希



我的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

cd Hash.c.each_with_index do |me,index|这里,每次迭代me首先具有 ["2","20"] 的值,然后是 ["3","30"] 。因此puts me将其打印为2 20 3 30。你需要看看Hash#[].

相关内容

最新更新