0

From this initialize array:

x = []
y = [1,2,3,4,5]
z = ["a", "b", "c", "d", "e"]

I want x result to be like this:

=> [[1,"a"], [2,"b"], [3,"c"], [4,"d"], [5,"e"]]

Is there any short method to do this without using nested loop?

2
  • 2
    y.zip(z) #=> [[1, "a"], [2, "b"], [3, "c"], [4, "d"], [5, "e"]] Commented Nov 10, 2022 at 10:12
  • [y, z].transpose # => => [[1, "a"], [2, "b"], [3, "c"], [4, "d"], [5, "e"]] or z.map.with_index { |_, idx| [y[idx], z[idx]] } # => same response Commented Nov 10, 2022 at 12:40

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.