Ruby | Array append() function
Last Updated :
07 Jan, 2020
Array#append() is an Array class method which add elements at the end of the array.
Syntax: Array.append()
Parameter: - Arrays for adding elements.
- elements to add
Return: Array after adding the elements at the end.
Example #1 :
Ruby
# Ruby code for append() method
# adding elements at the end
# 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]
# appending array or element at the end of the array
puts "adding elements in a : #{a.append(b)}\n\n"
puts "adding elements in b : #{b.append("ratttt")}\n\n"
puts "adding elements in c : #{c.append(b)}\n\n"
Output :
adding elements in a : [18, 22, 33, 4, 5, 6, [5, 4, 22, 1, 88, 9]]
adding elements in b : [5, 4, 22, 1, 88, 9, "ratttt"]
adding elements in c : [18, 22, 33, 40, 50, 6, [5, 4, 22, 1, 88, 9, "ratttt"]]
Example #2 :
Ruby
# Ruby code for append() method
# adding elements at the end
# declaring array
a = ["abc", "xyz", "dog"]
# declaring array
b = ["cow", "cat", "dog"]
# declaring array
c = ["cat", "1", "dog"]
# appending array or element at the end of the array
puts "adding elements in a : #{a.append(b)}\n\n"
puts "adding elements in b : #{b.append("ratttt")}\n\n"
puts "adding elements in c : #{c.append(b)}\n\n"
Output :
adding elements in a : ["abc", "xyz", "dog", ["cow", "cat", "dog"]]
adding elements in b : ["cow", "cat", "dog", "ratttt"]
adding elements in c : ["cat", "1", "dog", ["cow", "cat", "dog", "ratttt"]]
Similar Reads
Ruby | Array &() function Array#&() : &() is an Array class method which returns a new array containing unique elements common to the two arrays. Syntax: Array.&() Parameter: Array for the comparison Return: a new array containing unique elements common to the two arrays Example #1 : Ruby # Ruby code for &()
2 min read
Ruby | Array to_a() function Array#to_a() : to_a() is a Array class method which returns self array. Syntax: Array.to_a() Parameter: Array Return: self array representation Example #1 : Ruby # Ruby code for to_a() method # declaring array a = [18, 22, 33, nil, 5, 6] # declaring array b = [1, 4, 1, 1, 88, 9] # declaring array c
1 min read
Ruby | Array zip() function Array#zip() : zip() is a Array class method which Converts any arguments to arrays, then merges elements of self with corresponding elements from each argument. Syntax: Array.zip() Parameter: Array Return: merges elements of self with corresponding elements from each argument. Example #1 : Ruby # Ru
2 min read
Ruby | Array pop() function Array#pop() : pop() is a Array class method which checks removes the last element from the array and returns it. Syntax: Array.pop() Parameter: Array Return: removes the last element from the array and returns it. Example #1 : Ruby # Ruby code for pop() method # declaring array a = [18, 22, 33, nil,
1 min read
Ruby | Array map() function Array#map() : map() is a Array class method which returns a new array containing the values returned by the block. Syntax: Array.map() Parameter: Array Return: a new array containing the values returned by the block. Example #1 : Ruby # Ruby code for map() method # declaring array a = [18, 22, 33, 3
2 min read