# Kind of Array "product" question

I am cleaning up my code, which I wrote in the last months…
I have code like this:

irb(main):053:0* a = [‘a’, ‘b’, ‘c’]
=> [“a”, “b”, “c”]
irb(main):054:0> b = [1, 2]
=> [1, 2]
irb(main):055:0> a.map{|el| [el, b]}
=> [[“a”, 1, 2], [“b”, 1, 2], [“c”, 1, 2]] # (
)

The result is what I desire, but I had the feeling that this is not the
most elegant and fastest way, because it iterates over each element in
a. Is there a better solution to replace a.map… with?

I searched in the array documentation, but only found

irb(main):058:0> a.product([b])
=> [[“a”, [1, 2]], [“b”, [1, 2]], [“c”, [1, 2]]]

which is not really what I desire. Related question: What is the best
way to convert the double nested array above into (*) at the top?

Or can we write the following in a simpler form, without double array
indices?

a.product([b]).each{|el| c = el[0]; x = el[1][0]; y = el[1][1]; puts c,
x, y}

Best regards,

Stefan S.

On Nov 9, 2013, at 2:02 PM, Stefan S. [email protected] wrote:

The result is what I desire, but I had the feeling that this is not the
most elegant and fastest way, because it iterates over each element in
a. Is there a better solution to replace a.map… with?

I searched in the array documentation, but only found

irb(main):058:0> a.product([b])
=> [[“a”, [1, 2]], [“b”, [1, 2]], [“c”, [1, 2]]]

Simple one for this:

a.product([b]).map(&:flatten)

Still not elegant.