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
相關用法
- Ruby Range include?()用法及代碼示例
- Ruby Hash include?()用法及代碼示例
- Ruby Enumerable include?()用法及代碼示例
- Ruby String include?用法及代碼示例
- Ruby Array class include?()用法及代碼示例
- Ruby Set add?用法及代碼示例
- Ruby Enumerator each用法及代碼示例
- Scala Symbol =~用法及代碼示例
- Ruby Struct eql?()用法及代碼示例
- Ruby Hash each()用法及代碼示例
- Scala Symbol ==用法及代碼示例
- Ruby DateTime sec()用法及代碼示例
注:本文由純淨天空篩選整理自gopaldave大神的英文原創作品 Ruby | Set include?() function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。