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


Ruby Array class include?()用法及代码示例


Array#include?():include?()是一个Array类方法,用于检查参数对象是否存在于数组中。

用法: Array.include?()

参数:obj-要查找的元素


返回:true-如果元素存在;否则为假

代码#1:include?()方法的示例

# Ruby code for include?() method 
  
# declaring array 
a = [18, 22, 33, nil, 5, 6] 
  
# declaring array 
b = [1, 4, 1, 1, 88, 9] 
  
# declaring array 
c = [18, 22, nil, nil, 50, 6] 
  
# include? 
puts "include?:#{a.include?(5)}\n\n"
  
# include? 
puts "include?:#{b.include?(0)}\n\n"
  
# include? 
puts "include?:#{c.include?(nil)}\n\n"

输出:

include?:true

include?:false

include?:true

代码2:include?()方法的示例

# Ruby code for include?() method 
  
# declaring array 
a = ["abc", "nil", "dog"] 
  
# declaring array 
b = ["cow", nil, "dog"] 
  
# declaring array 
c = ["cat", nil, nil] 
  
# include? 
puts "include?:#{a.include?("cat")}\n\n"
  
# include? 
puts "include?:#{b.include?("nil")}\n\n"
  
# include? 
puts "include?:#{c.include?("cat")}\n\n"

输出:

include?:false

include?:false

include?:true


相关用法


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