Integer literal in flip-flop

hi!
I just wrote this short conditional statement and I got warning: integer literal in flip-flop
What does this mean and how can I avoid it?

x=8
if x===1..18
 puts "minor"
else
 puts "major"
end

The flip flop operator is a strange feature of Ruby that not many people know about. It’s essentially a range with comparators, e.g. (x==5)..(x==10). When used in loops, it allows “turning on” the range at the first comparison, then “turning off” the range at the second comparison. It’s not widely used, and generally discouraged because it’s hard to read and understand. See https://docs.rubocop.org/rubocop/cops_lint.html#lintflipflop.

The Ruby interpreter is putting === ahead of .. in terms of precedence, so it sees your code as (x===1)..18, thus thinking you are using a flip flop operator on the left hand side of the .., but an integer literal on the right hand side of the flip flop operator.

To fix the precedence, just put your range in parenthesis to fix: x===(1..18).

However, that still won’t work, because the === method you want is on Range, not on Integer. So, to fix that, just reverse it to (1..18)===x, or use the .include? or .cover? method: (1..18).include?(x)