I have three Ruby arrays:
color = ['blue', 'green', 'yellow']
names = ['jack', 'jill']
combination = []
I need the following concatenation inserted into the combination
array:
FOR EACH names value: [name value] + " wants " + [color value]
So the outcome will be:
combination = ['jack wants blue','jack wants green','jack wants yellow','jill wants blue','jill wants green','jill wants yellow']
I can't figure out how to do this. I've tried this to start off with but no avail:
name.each do |name|
puts "#{name} wants #{color}"
end
You can use Array#product
:
names = ['jack', 'jill']
colors = ['blue', 'green', 'yellow']
names.product(colors).map { |name, color| "#{name} wants #{color}" }
#=> ["jack wants blue", "jack wants green", "jack wants yellow", "jill wants blue", "jill wants green", "jill wants yellow"]
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments