红宝石 |设置为空?()函数
empty?()是 Ruby 中的一个内置方法,如果集合为空,则返回 true,否则返回 false。
Syntax: s1.empty?()
Parameters: The function does not takes any parameter.
Return Value: It returns a boolean value. It returns true if the set is empty or it returns false.
示例 1 :
# Ruby program to illustrate the empty method
# requires the set
require "set"
s1 = Set[]
# empty method used
puts "is empty?:"
puts s1.empty?()
输出:
is empty?:
true
示例 2 :
# Ruby program to illustrate the empty method
# requires the set
require "set"
s1 = Set[16, 8, 3, 5, 2]
# empty method used
puts "is empty?:"
puts s1.empty?()
输出:
false