0

*newbie apologies..

The following code:

dates = ['Transaction_Date','Renewal_Date']
dateoptions = ['Year','Month','Date']
word1 = "Amount"

dates.each do |x|
  puts word1 + " by " + x
  end

Returns

Amount by Transaction_Date
Amount by Renewal_Date

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.

Appreciate your input

4 Answers 4

3

You need to use nested 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

Or you can use Array#product

dates.product(dateoptions) do |d, option|
  puts "#{word1} by #{d} (#{option})"
end
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect, both options help me a lot. Thank you @falsetru
2

You can use Array#product method:

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

Comments

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

Comments

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.