Ruby | Array push() function
Last Updated :
06 Dec, 2019
Array#push() : push() is a Array class method which appends the given object(s) on to the end of this array.
Syntax: Array.push()
Parameter: Array
Return: appends the given object(s) on to the end of this array.
Example #1 :
a = [ 18 , 22 , 33 , nil , 5 , 6 ]
b = [ 1 , 4 , 1 , 1 , 88 , 9 ]
c = [ 34 , 5 , 7 ]
puts "push() method form : #{a.push(3)}\n\n"
puts "push() method form : #{b.push()}\n\n"
puts "push() method form : #{c.push(2)}\n\n"
|
Output :
push() method form : [18, 22, 33, nil, 5, 6, 3]
push() method form : [1, 4, 1, 1, 88, 9]
push() method form : [34, 5, 7, 2]
Example #2 :
a = [ "abc" , "nil" , "dog" ]
c = [ nil , "abc" , "nil" , "dog" ]
b = [ "cow" , nil , "dog" ]
puts "push() method form : #{a.push(2)}\n\n"
puts "push() method form : #{b.push(1)}\n\n"
puts "push() method form : #{c.push(0)}\n\n"
|
Output :
push() method form : ["abc", "nil", "dog", 2]
push() method form : ["cow", nil, "dog", 1]
push() method form : [nil, "abc", "nil", "dog", 0]
Share your thoughts in the comments
Please Login to comment...