Ruby array include? Multiple options.

Posted by John Maddux Published on June 28, 2014

I was working on trying to find if an array had an element in it. So, of course, I started with:

[1, 2, 3].include?(x)


This gave me a true/false for if x was in the array. Naturally.

Next I wanted to see if either x or y was in the array. I tried:

[1, 2, 3].include?(x || y) 

=> didn’t work


[1, 2, 3].include?(x) || [1, 2, 3].include?(y)

=> Worked but doesn’t scale very well.


In the end I came up with:

[x, y].any?{|a| [1, 2, 3].include?(a)}


Now I can see if any number out of an array ([x, y]) is inside the other array ([1, 2, 3]).