红宝石 |数组类包含?() 操作
Array#include?() : include?()是一个 Array 类方法,用于检查参数对象是否存在于数组中。
Syntax: Array.include?()
Parameter: obj – element to find
Return: true – if the element is present; otherwise false
代码 #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