Ruby 数组的pop()函数
Array#pop() : pop()是一个数组类方法,它检查从数组中删除最后一个元素并返回。
语法。Array.pop()
参数。数组
返回:从数组中删除最后一个元素,并将其返回。
例子 #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 : []