红宝石 |范围 last()函数
last()是 Ruby 中的一个内置方法,返回最后一个 X 元素的数组。如果未提及 X,则仅返回最后一个元素。
Syntax: range1.last(X)
Parameters: The function accepts X which is the number of elements from the end.
Return Value: It returns an array of last X elements.
示例 1 :
# Ruby program for last()
# method in Range
# Initialize range
range1 = (0..10)
# Prints the last element
puts range1.last()
输出:
10
示例 2 :
# Ruby program for last()
# method in Range
# Initialize range
range1 = (0..10)
# Prints the last 3 elements
puts range1.last(3)
输出:
8
9
10