Ruby 矩阵类的include?()操作
Array#include?() : include?()是一个数组类方法,用于检查被论证的对象是否存在于数组中。
语法。Array.include?()
参数:obj – 要查找的元素
返回:true – 如果该元素存在;否则为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