红宝石嵌入到每个做+阵列串联 [英] Ruby nested each do + array concatenation

查看:116
本文介绍了红宝石嵌入到每个做+阵列串联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

*新手歉意。

以下code:

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

我想结果是日期和dateoptions的串联,像这样

I would like the outcome to be a concatenation of the dates and dateoptions, like so

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)

我想在做一个嵌套的每一个做,但我仍然不知道如何处理这两个数组的串联。

I was thinking of doing a nested "each do" but I still wouldn't know how to address the concatenation of the two arrays.

鸭preciate你输入

Appreciate your input

推荐答案

您需要使用嵌套的每个

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

您也可以使用 阵列#产品

dates.product(dateoptions) do |d, option|
  puts "#{word1} by #{d} (#{option})"
end

这篇关于红宝石嵌入到每个做+阵列串联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆