The answer is due to their precedence relative to the #include?
method call without parentheses.
or
binds less tightly (has lower precedence, i.e. evaluates later) than the method call .include? n
, and so the expression evaluates as ([].include? n) or (n == 1) #=> true
||
, on the other hand, binds more tightly (has higher precedence, i.e. evaluates earlier) than the method call, and so the expression evaluates as [].include?((n || (n == 1))) #=> false
.
Precedence refers to the order of operations. It's like in algebra class when you learned PEMDAS:
- parentheses,
- exponentiation,
- multiplication/division,
- addition/subtraction.
Programming languages, like mathematical notation, have to obey a specific order of operations in order to evaluate non-ambiguously. In Ruby, for the operations you have here, it goes:
||
,- method call,
or
.