📜  红宝石 |数组 pop()函数

📅  最后修改于: 2022-05-13 01:54:54.713000             🧑  作者: Mango

红宝石 |数组 pop()函数

Array#pop() : pop()是一个 Array 类方法,它检查从数组中删除最后一个元素并返回它。

示例 #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"

输出 :

pop() method form : 6

pop() method form : 9

pop() method form : [5, 7]

示例 #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"

输出 :

pop() method form : ["nil", "dog"]

pop() method form : ["dog"]

pop() method form : []