红宝石 |范围 to_a()函数
to_a()是 Ruby 中的一个内置方法,它返回一个包含给定范围内数字的数组。
Syntax: range1.to_a()
Parameters: The function accepts no parameter.
Return Value: It returns an array containing all the numbers.
示例 1 :
# Ruby program for to_a()
# method in Range
# Initialize range
range1 = (0..4)
# Prints the array
puts range1.to_a()
输出:
0
1
2
3
4
示例 2 :
# Ruby program for to_a()
# method in Range
# Initialize range
range1 = (7..9)
# Prints the array
puts range1.to_a()
输出:
7
8
9