5

Possible Duplicates:
i = true and false in Ruby is true?
What is the difference between Perl's ( or, and ) and ( ||, && ) short-circuit operators?
Ruby: difference between || and 'or'

Is || same as or in Rails?

Case A:

 @year = params[:year] || Time.now.year
 Events.all(:conditions => ['year = ?', @year])

will produce the following SQL in script/console:

 SELECT * FROM `events` WHERE (year = 2000)

Case B:

 @year = params[:year] or Time.now.year
 Events.all(:conditions => ['year = ?', @year])

will produce the following SQL in script/console:

 SELECT * FROM `events` WHERE (year = NULL)
Community
  • 1
  • 1
ohho
  • 50,879
  • 75
  • 256
  • 383
  • 3
    Same as the question [i = true and false in Ruby is true?](http://stackoverflow.com/questions/2802494/i-true-and-false-in-ruby-is-true) except with `or` instead of `and`. – sepp2k Oct 11 '10 at 09:17
  • 2
    Duplicate to: http://stackoverflow.com/questions/3826112/in-ruby-should-we-always-use-instead-of-and-or-unless-for-specia/3828955#3828955, http://stackoverflow.com/questions/1512547/what-is-the-difference-between-perls-or-and-and-short-circuit-op and probably many more. – draegtun Oct 11 '10 at 10:08
  • 3
    This question has already been asked and answered in http://StackOverflow.Com/q/2083112/, http://StackOverflow.Com/q/1625946/, http://StackOverflow.Com/q/1426826/, http://StackOverflow.Com/q/1840488/, http://StackOverflow.Com/q/1434842/, http://StackOverflow.Com/q/2376369/, http://StackOverflow.Com/q/2802494/, http://StackOverflow.Com/q/372652/. – Jörg W Mittag Oct 11 '10 at 13:26
  • @Andrew can you suggest the keywords to search duplicates for this question? I am not sure how to make the search works for `||` and `or`. – ohho Oct 15 '10 at 01:56
  • I put it as another question: http://stackoverflow.com/questions/3938961/how-to-search-and-or-on-stackoverflow – ohho Oct 15 '10 at 02:03
  • @ohho: If you wrote a question with a title "Ruby and or operators", you get [Understanding the “||” OR operator in If conditionals in Ruby](http://stackoverflow.com/questions/1554340/understanding-the-or-operator-in-if-conditionals-in-ruby) – Andrew Grimm Oct 15 '10 at 02:06
  • @Andrew are you sure the http://stackoverflow.com/questions/1554340/understanding-the-or-operator-in-if-conditionals-in-ruby question talks about the difference between `||` and `or`? – ohho Oct 15 '10 at 02:37

2 Answers2

14

The reason that || and or behave differently is because of operator precedence.

Both || and && have higher precedence than the assignment operator and the assignment operator (=) has higher precedence than and/or

So your expressions will actually be evaluated as follows :-

@year = params[:year] || Time.now.year

is evaluated as

@year = ( params[:year] || Time.now.year )

and

@year = params[:year] or Time.now.year

is evaluated as

( @year = params[:year] ) or Time.now.year

If in doubt about precedence rules then use parentheses to make your meaning clear.

Steve Weet
  • 28,126
  • 11
  • 70
  • 86
4

Quote from http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Operators:

The binary "or" operator will return the logical disjunction of its two operands. It is the same as "||" but with a lower precedence.

a = nil
b = "foo"
c = a || b  # c is set to "foo" its the same as saying c = (a || b)
c = a or b  # c is set to nil   its the same as saying (c = a) || b which is not what you want.

So you or works as:

(@year = params[:year]) or Time.now.year

So params[:year] is assigned to @year, and second part of expression is not assigned to anything. You should use explicit brackets if you want to use or:

@year = (params[:year] or Time.now.year)

And this is the difference.

Andrey
  • 59,039
  • 12
  • 119
  • 163