*新手道歉......
以下代码:
dates = ['Transaction_Date','Renewal_Date']
dateoptions = ['Year','Month','Date']
word1 = "Amount"
dates.each do |x|
puts word1 + " by " + x
end
返回
Amount by Transaction_Date
Amount by Renewal_Date
我希望结果是日期和日期选项的串联,如此
Amount by Transaction_Date (Year)
Amount by Transaction_Date (Month)
Amount by Transaction_Date (Date)
Amount by Renewal_Date (Year)
Amount by Renewal_Date (Month)
Amount by Renewal_Date (Month)
我正在考虑做一个嵌套的“每个做”,但我仍然不知道如何处理两个数组的连接。
感谢您的输入
答案 0 :(得分:3)
您需要使用嵌套的each
:
dates = ['Transaction_Date','Renewal_Date']
dateoptions = ['Year','Month','Date']
word1 = "Amount"
dates.each do |d|
dateoptions.each do |option|
puts "#{word1} by #{d} (#{option})"
end
end
或者您可以使用Array#product
dates.product(dateoptions) do |d, option|
puts "#{word1} by #{d} (#{option})"
end
答案 1 :(得分:2)
您可以使用Array#product
方法:
dates.product(dateoptions).each do |date, option|
puts "#{word1} by #{date} (#{option})"
end
答案 2 :(得分:0)
dates = ['Transaction_Date','Renewal_Date']
dateoptions = ['Year','Month','Date']
word1 = "Amount"
dates.each do |x|
dateoptions.each do |y|
puts "#{word1} by #{x} (#{y})"
end
end
答案 3 :(得分:0)
2.1.2 :009 > dates.each do |e|
2.1.2 :010 > dateoptions.each do |o|
2.1.2 :011 > puts "#{word1} by #{e} (#{o})"
2.1.2 :012?> end
2.1.2 :013?> end
Amount by Transaction_Date (Year)
Amount by Transaction_Date (Month)
Amount by Transaction_Date (Date)
Amount by Renewal_Date (Year)
Amount by Renewal_Date (Month)
Amount by Renewal_Date (Date)