当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Ruby Set length()用法及代码示例


length()是Ruby中的内置方法,它返回Set的大小。它返回集合中的元素数。

用法: s1_name.length()

参数:该函数不接受任何参数。


返回值:返回集合的大小。

例子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


相关用法


注:本文由纯净天空筛选整理自gopaldave大神的英文原创作品 Ruby | Set length() function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。