Difference between “or” and || in Ruby?

2018-12-31 14:39发布

问题:

What\'s the difference between the or and || operators in Ruby? Or is it just preference?

回答1:

It\'s a matter of operator precedence.

|| has a higher precedence than or.

So, in between the two you have other operators including ternary (? :) and assignment (=) so which one you choose can affect the outcome of statements.

Here\'s a ruby operator precedence table.

See this question for another example using and/&&.

Also, be aware of some nasty things that could happen:

a = false || true
=> true
a
=> true

a = false or true
=> true
a
=> false

Both of the previous two statements evaluate to true, but the second sets a to false since = precedence is lower than || but higher than or.



回答2:

As the others have already explained, the only difference is the precedence. However, I would like to point out that there are actually two differences between the two:

  1. and, or and not have much lower precedence than &&, || and !
  2. and and or have the same precedence, while && has higher precedence than ||

In general, it is good style to avoid the use of and, or and not and use &&, || and ! instead. (The Rails core developers, for example, reject patches which use the keyword forms instead of the operator forms.)

The reason why they exist at all, is not for boolean formulae but for control flow. They made their way into Ruby via Perl\'s well-known do_this or do_that idiom, where do_this returns false or nil if there is an error and only then is do_that executed instead. (Analogous, there is also the do_this and then_do_that idiom.)

Examples:

download_file_via_fast_connection or download_via_slow_connection
download_latest_currency_rates and store_them_in_the_cache

Sometimes, this can make control flow a little bit more fluent than using if or unless.

It\'s easy to see why in this case the operators have the \"wrong\" (i.e. identical) precedence: they never show up together in the same expression anyway. And when they do show up together, you generally want them to be evaluated simply left-to-right.



回答3:

and/or are for control flow.

Ruby will not allow this as valid syntax:

false || raise \"Error\"

However this is valid:

false or raise \"Error\"

You can make the first work, with () but using or is the correct method.

false || (raise \"Error\")


回答4:

puts false or true 

prints: false

puts false || true 

prints: true

Yoda: Careful you must be.



回答5:

The way I use these operators:

||, && are for boolean logic. or, and are for control flow. E.g.

do_smth if may_be || may_be -- we evaluate the condition here

do_smth or do_smth_else -- we define the workflow, which is equivalent to do_smth_else unless do_smth

to give a simple example:

> puts \"a\" && \"b\" b

> puts \'a\' and \'b\' a

A well-known idiom in Rails is render and return. It\'s a shortcut for saying return if render, while render && return won\'t work (see Rails documentation)



回答6:

or is NOT the same as ||

Use only || operator instead of or operator.

Reasons :

  • or operator have lower precedence than ||
  • or have lower precedence than = assignment operator
  • and and or have the same precedence, while && has higher precedence than ||


回答7:

Both \"or\" and \"||\" evaluate to true if either operand is true. They evaluate their second operand only if the first is false.

As with \"and\", the only difference between \"or\" and \"||\" is their precedence.

Just to make life interesting, \"and\" and \"or\" have the same precedence, while \"&&\" has a higher precedence than \"||\".



回答8:

Just to add to mopoke\'s answer, it\'s also a matter of semantics. or is considered to be a good practice because it reads much better than ||.