R
R
rusrich2019-01-05 00:20:35
ruby
rusrich, 2019-01-05 00:20:35

How to add an element from another array to each element of an array in Ruby?

There are two arrays

array1 = Array.[]("a", "b", "c", "d", "e")
array2 = Array.[]("1", "2", "3", "4", "5")

It is necessary to bring the output to the following form:
"//1a or //2b or //3c or //4d or //5e"
Started like this
a = array2.all.map {  | w|  "//"  + w + ....... }
a.join(" or ")

The output of one array turns out to be normal, but I don’t know how to add elements of the second array in turn to each element of the array.
The number of elements in the arrays is the same.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2019-01-05
@rusrich

a = array1.map.with_index{|n, i| '//' + array2[i] + n}.join(' or ')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question