Other methods:
A,B,C = 1,2,3 arr = [A, B, C] x = 2 y = 4
Use Array # &
[x] & arr == [x] #=> true [y] & arr == [y] #=> false
Use Array # -
([x] - arr).empty? #=> true ([y] - arr).empty? #=> false
Use Array # |
arr | [x] == arr #=> true arr | [y] == arr
Use Array # index
arr.index(x) #=> 1 (truthy) arr.index(y) #=> nil (falsy) !!arr.index(x) #=> true !!arr.index(y) #=> false
Use the @edgerunner generalized solution
def present?(arr, o) case o when *arr puts "#{o} matched" else puts "#{o} not matched" end end present?(arr, x)
Cary swoveland
source share