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
相關用法
- Ruby Array class each()用法及代碼示例
- Ruby Array class eql?()用法及代碼示例
- Ruby Array class fill()用法及代碼示例
- Ruby Array class each_index()用法及代碼示例
- Ruby Array class fetch()用法及代碼示例
- Ruby Array class empty?()用法及代碼示例
- Ruby Array class find_index()用法及代碼示例
- Ruby Array class frozen?()用法及代碼示例
- Ruby Array class drop_while()用法及代碼示例
- Ruby Array at()用法及代碼示例
- Ruby Array dig()用法及代碼示例
- Ruby Array map!()用法及代碼示例
- Ruby Array any?()用法及代碼示例
- Ruby Array rassoc()用法及代碼示例
- Ruby Array cycle()用法及代碼示例
注:本文由純淨天空篩選整理自mayank5326大神的英文原創作品 Ruby | Array class include?() operation。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。