Hello Guys,
Want the combination of array elements using ruby? than use the below custom method.
def arr_combine(arr_1, arr_2)
return arr_1 if arr_2.empty?
arr_3 = []
arr_1.each do |e1|
arr_2.each do |e2|
arr_3 << [e1, e2].flatten
end
end
return arr_3
end
return arr_1 if arr_2.empty?
arr_3 = []
arr_1.each do |e1|
arr_2.each do |e2|
arr_3 << [e1, e2].flatten
end
end
return arr_3
end
eg. we have array like arr = [[1,2], [3,4,5]]
we expect the result as [[1,3],[1,4],[1,5],[2,3],[2,4],[2,5]] than simply invoke the above method by passing each element of given array.
arr_combine(arr[0], arr[1]) gives you required outcome.
Hope this post will help you out.
No comments:
Post a Comment