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 :
Ruby
# Ruby code for +() method
# showing concatenate operation
# declaring array
a = [18, 22, 33, 4, 5, 6]
# declaring array
b = [5, 4, 22, 1, 88, 9]
# declaring array
c = [18, 22, 33, 40, 50, 6]
# combining arrays
puts "combination of a and b : #{a + b}\n\n"
# combining arrays
puts "combination of a and c : #{a + c}\n\n"
# combining arrays
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 :
Ruby
# Ruby code for +() method
# showing concatenate operation
# declaring array
a = ["abc", "xyz", "dog"]
# declaring array
b = ["cow", "cat", "dog"]
# declaring array
c = ["cat", "1", "dog"]
# combining arrays
puts "combination of a and b : #{a + b}\n\n"
# combining arrays
puts "combination of a and c : #{a + c}\n\n"
# combining arrays
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"]
Similar Reads
Ruby | Array values_at() function Array#values_at() : values_at() is a Array class method which returns an array containing the elements in self corresponding to the given selector. Syntax: Array.values_at() Parameter: Array Return: an array containing the elements in self corresponding to the given selector Example #1 : Ruby # Ruby
1 min read
Ruby | Array repeated_combination() function Array#repeated_combination() : repeated_combination() is a Array class method which returns all repeated combinations of length n of elements from the array and then returns the array itself. Syntax: Array.repeated_combination() Parameter: Array Return: all repeated combinations of length n of eleme
2 min read
Ruby | Array concat() operation Array#concat() : concat() is a Array class method which returns the array after appending the two arrays together. Syntax: Array.concat() Parameter: Arrays to be combined Return: Append the two arrays Code #1 : Example for concat() method Ruby # Ruby code for concat() method # adding elements at the
2 min read
Ruby | Array class to_s() function to_s() is an Array class method which returns the string representation of the array elements. Syntax: Array.to_s() Parameter: Array Return: string representation of the array elements. Example #1: Ruby # Ruby code for to_s() method # declaring array a = [18, 22, 33, nil, 5, 6] # declaring array b =
1 min read
Ruby | Array unshift() function Array#unshift() : unshift() is a Array class method which returns the shifted array with argumented element in place. Syntax: Array.unshift()Parameter: ArrayReturn: the shifted array with argumented element in place. Example #1 : Ruby # Ruby code for unshift() method # declaring array a = [18, 22, 3
2 min read
Ruby | Array uniq() function Array#uniq() : uniq() is a Array class method which returns a new array by removing duplicate values in the array. Syntax: Array.uniq() Parameter: Array Return: a new array by removing duplicate values in the array Example #1 : Ruby # Ruby code for uniq() method # declaring array a = [18, 22, 33, ni
2 min read