Ruby | Array append() function
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 addReturn: Array after adding the elements at the end.
Example #1 :
# Ruby code for append() method# adding elements at the end # declaring arraya = [18, 22, 33, 4, 5, 6] # declaring arrayb = [5, 4, 22, 1, 88, 9] # declaring arrayc = [18, 22, 33, 40, 50, 6] # appending array or element at the end of the arrayputs "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 code for append() method# adding elements at the end # declaring arraya = ["abc", "xyz", "dog"] # declaring arrayb = ["cow", "cat", "dog"] # declaring arrayc = ["cat", "1", "dog"] # appending array or element at the end of the arrayputs "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"]]


.png)