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