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