红宝石 |设置长度()函数
length()是 Ruby 中的一个内置方法,它返回 Set 的大小。它返回集合中元素的数量。
Syntax: s1_name.length()
Parameters: The function does not accepts any parameter.
Return Value: It returns the size of the set.
示例 1 :
# Ruby program to illustrate
# the length method
# requires the set
require "set"
s1 = Set[16, 8, 3, 5, 2]
# length method used
puts s1.length()
输出:
5
示例 2 :
# Ruby program to illustrate
# the length method
# requires the set
require "set"
s1 = Set[1, 2, 5]
# length method used
puts s1.length()
输出:
3