The pop() function in Ruby is used to pop or remove the last element of the given array and returns the removed elements.
Syntax: pop(Elements)
Parameters:
Elements : This is the number of elements which are to be removed from the end of the given array. If this parameter is not used then it removes and returns the single last element of the given array.
Returns: the removed elements.
Example 1:
Array = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 ]
A = Array .pop
B = Array .pop( 2 )
C = Array .pop( 3 )
D = Array
puts "#{A}"
puts "#{B}"
puts "#{C}"
puts "#{D}"
|
Output:
7
[5, 6]
[2, 3, 4]
[1]
Example 2:
Array = [ "a" , "b" , "c" , "d" , "e" , "f" , "g" ]
A = Array .pop
B = Array .pop( 1 )
C = Array .pop( 2 )
D = Array .pop( 3 )
E = Array
puts "#{A}"
puts "#{B}"
puts "#{C}"
puts "#{D}"
puts "#{E}"
|
Output:
g
["f"]
["d", "e"]
["a", "b", "c"]
[]
Reference: https://devdocs.io/ruby~2.5/array#method-i-pop
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
06 May, 2019
Like Article
Save Article