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)
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.
Quote from http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Operators:
So you
or
works as: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:And this is the difference.