红宝石 |范围条目()函数
entry()是 Ruby 中的一个内置方法,它返回一个包含给定范围内所有元素的数组。
Syntax: range1.entries()
Parameters: The function accepts no parameter.
Return Value: It returns an array containing all elements of the given range.
示例 1 :
# Ruby program for entries()
# method in Range
# Initialize range
range1 = (0..10)
# Stores in array
arr = range1.entries()
# Prints elements
puts arr
输出:
0
1
2
3
4
5
6
7
8
9
10
示例 2 :
# Ruby program for entries()
# method in Range
# Initialize range
range1 = (2..4)
# Stores in array
arr = range1.entries()
# Prints elements
puts arr
输出:
2
3
4