Ruby 集合 include?()函数
include?() 是Ruby中一个内置的方法,如果集合包含给定的对象,则返回true。如果它不包含给定的对象,则返回false。
语法 :s1_name.include?(object)
参数 :该函数接受一个强制参数对象,该对象的存在将被检查。
返回值 : 如果该集合包含给定的对象,则返回true,否则返回false。
例子 1 :
# Ruby program to illustrate
# the include method
# requires the set
require "set"
s1 = Set[16, 8, 3, 5, 2]
# include method used
puts s1.include?(2)
输出:
true
例2 :
# Ruby program to illustrate
# the include method
# requires the set
require "set"
s1 = Set[1, 2, 3]
# include method used
puts s1.include?(5)
输出:
false