Open In App

Ruby | Array pop() function

Improve
Improve
Like Article
Like
Save
Share
Report

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 code for pop() method
  
# declaring array
a = [18, 22, 33, nil, 5, 6]
  
# declaring array
b = [1, 4, 1, 1, 88, 9]
  
# declaring array
c = [34, 5, 7]
  
# pop method example
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 :




# Ruby code for pop() method
  
# declaring array
a = ["abc", "nil", "dog"]
  
# declaring array
c = [nil, "abc", "nil", "dog"]
  
# declaring array
b = ["cow", nil, "dog"]
  
  
# pop method example
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 : []



Last Updated : 06 Dec, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads