Ruby | Array Concatenation using (+) function
Last Updated :
07 Jan, 2020
Array#+() is a Array class method which performs set concatenate operation arrays by combining two arrays to a third array.
Syntax: Array.+()
Parameter: Arrays for performing the concatenation operation.
Return: New arrays by combining two arrays.
Example #1 :
a = [ 18 , 22 , 33 , 4 , 5 , 6 ]
b = [ 5 , 4 , 22 , 1 , 88 , 9 ]
c = [ 18 , 22 , 33 , 40 , 50 , 6 ]
puts "combination of a and b : #{a + b}\n\n"
puts "combination of a and c : #{a + c}\n\n"
puts "combination of b and c : #{b + c}\n\n"
|
Output :
combination of a and b : [18, 22, 33, 4, 5, 6, 5, 4, 22, 1, 88, 9]
combination of a and c : [18, 22, 33, 4, 5, 6, 18, 22, 33, 40, 50, 6]
combination of b and c : [5, 4, 22, 1, 88, 9, 18, 22, 33, 40, 50, 6]
Example #2 :
a = [ "abc" , "xyz" , "dog" ]
b = [ "cow" , "cat" , "dog" ]
c = [ "cat" , "1" , "dog" ]
puts "combination of a and b : #{a + b}\n\n"
puts "combination of a and c : #{a + c}\n\n"
puts "combination of b and c : #{b + c}\n\n"
|
Output :
combination of a and b : ["abc", "xyz", "dog", "cow", "cat", "dog"]
combination of a and c : ["abc", "xyz", "dog", "cat", "1", "dog"]
combination of b and c : ["cow", "cat", "dog", "cat", "1", "dog"]
Share your thoughts in the comments
Please Login to comment...