Why || and or behaves differently in rails? [dupli

2020-07-22 19:23发布

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)

2条回答
贼婆χ
2楼-- · 2020-07-22 19:52

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.

查看更多
冷血范
3楼-- · 2020-07-22 19:57

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.

查看更多
登录 后发表回答