红宝石 |范围大小()函数
size()是 Ruby 中的一个内置方法,用于返回范围的大小。它返回范围内的元素数。
Syntax: range1.size()
Parameters: The function accepts no parameter.
Return Value: It returns the number of elements in the range.
示例 1 :
# Ruby program for size()
# method in Range
# Initialize range
range1 = (0..10)
# Prints the size element
puts range1.size()
输出:
11
示例 2 :
# Ruby program for size()
# method in Range
# Initialize range
range1 = (4..98)
# Prints the size of elements
puts range1.size()
输出:
95