Ruby | Array pop() function
Last Updated :
06 Dec, 2019
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 :
a = [ 18 , 22 , 33 , nil , 5 , 6 ]
b = [ 1 , 4 , 1 , 1 , 88 , 9 ]
c = [ 34 , 5 , 7 ]
puts "pop() method form : #{a.pop()}\n\n"
puts "pop() method form : #{b.pop()}\n\n"
puts "pop() method form : #{c.pop(2)}\n\n"
|
Output :
pop() method form : 6
pop() method form : 9
pop() method form : [5, 7]
Example #2 :
a = [ "abc" , "nil" , "dog" ]
c = [ nil , "abc" , "nil" , "dog" ]
b = [ "cow" , nil , "dog" ]
puts "pop() method form : #{a.pop(2)}\n\n"
puts "pop() method form : #{b.pop(1)}\n\n"
puts "pop() method form : #{c.pop(0)}\n\n"
|
Output :
pop() method form : ["nil", "dog"]
pop() method form : ["dog"]
pop() method form : []
Share your thoughts in the comments
Please Login to comment...