11 March 2016 - Forum Rules

Main Menu

Precedence of and/or versus method arguments in ruby

Started by Maryellen02, April 20, 2019, 01:53:43 AM

Previous topic - Next topic


Here are two tests:

if [1,2,3,4].include? 2 && nil.nil?
  puts :hello


if [1,2,3,4].include?(2) && nil.nil?
  puts :hello
#=> hello

The above tells me that && has higher precedence than method arguments so it logically ands 2 && nil.nil? which is true and passes that as an argument to include?.

However, there is this test:

if [1,2,3,4].include? 2 and nil.nil?
  puts :hello
#=> hello

So this is telling me that method arguments and 'and' have the same precedence (or method args are higher than 'and') since it passed 2 to include? before it processed 'and'.

Note: I understand that && and and have different precedence. The question is not regarding this but regarding and or or vs the arguments to a ruby method.

I can't find documentation that affirms this. For instances, this doesn't mention method arguments at all: or

Could anyone explain this behavior? Namely in that how does ruby know to pass values as arguments to a method vs. process operators?