12
a = [1, 2, 3]
b = [4, 5, 6]

How would I combine the two arrays in a 2D array?:

[[1, 4], [2, 5], [3, 6]]

2 Answers 2

32

Try Array#zip

a.zip(b)
=> [[1,4],[2,5],[3,6]]
Sign up to request clarification or add additional context in comments.

1 Comment

Tx. Kulbir! Didn't know that existed.
10

While zip is obviously the most straightforward answer, this also works:

[a, b].transpose
=> [[1, 4], [2, 5], [3, 6]]

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.