News: 11 March 2016 - Forum Rules
Current Moderators - DarkSol, KingMike, MathOnNapkins, Azkadellia, Danke

Author Topic: Precedence of and/or versus method arguments in ruby  (Read 242 times)

Maryellen02

  • Newbie
  • *
  • Posts: 1
    • View Profile
Precedence of and/or versus method arguments in ruby
« on: April 20, 2019, 01:53:43 am »
Here are two tests:

Code: [Select]
if [1,2,3,4].include? 2 && nil.nil?
  puts :hello
end
#=>

and

Code: [Select]
if [1,2,3,4].include?(2) && nil.nil?
  puts :hello
end
#=> 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:

Code: [Select]
if [1,2,3,4].include? 2 and nil.nil?
  puts :hello
end
#=> 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: http://phrogz.net/programmingruby/language.html#table_18.4 or http://romhack.wikia.com/wiki/Ruby_operators.

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