Ruby - array multiplication or JOIN operation

I have two arrays,

a = [1, 2]
b = [:a]

I want to get the result as

[[1, :a], [2, :a]]

Are there any methods for this?

+3
source share
2 answers

Use Array # product :

a = [1, 2]
b = [:a]
a.product(b)
=> [[1, :a], [2, :a]]
+6
source

You can also do it this way.

[a,b*a.size].transpose
#=> [[1, :a], [2, :a]]
0
source

All Articles