6

Possible Duplicate:
Ruby: difference between || and ‘or’

Using Ruby

||

and

or 

are very common practices which makes it important to know the difference between the two as unfortunately I am not sure.

First of all my question is if the following assumption is correct:

EX1:

if @variable_1 || @variable_2 || @variable_3 
  do something 
else
  do nothing
end

EX2:

if @variable_1 or @variable_2 or @variable_3
  do something 
else
  do nothing
end

So in the first example if any variable is false then it will execute "do nothing"

However, for the second example all variables are checked and if one is true then it will execute "do something".

In summary use "||" if you have a list of variables that need to be checked and if one of them returns false then a flag goes up. Use the second example with a list of variables where only one needs to be true in order to continue executing the desired code.

Are these assumptions correct?

Community
  • 1
  • 1
thenengah
  • 42,557
  • 33
  • 113
  • 157
  • Exact duplicate of http://StackOverflow.Com/questions/2083112/ and http://StackOverflow.Com/questions/1625946/. Semantically equivalent to http://StackOverflow.Com/questions/1426826/ and http://StackOverflow.Com/questions/1840488/. Also (somewhat) answered in http://StackOverflow.Com/questions/1434842/. Seriously? What earth-shattering changes in the Ruby Language have there been in the last 6 weeks that this same question needs to be answered over and over and over and over again? – Jörg W Mittag Mar 04 '10 at 21:39
  • @Jörg: I'd expect SO regulars to know that it's been asked before, but I'm not surprised at a newbie not being able to google the related questions. – Andrew Grimm May 10 '10 at 23:32

3 Answers3

14

or the second example all variables are checked and if one is true then it will execute "do something".

This is false sentence.

As a result your assumptions are not correct.

Both or and || do the same thing.

The main difference is that or has lower precedence than ||. So you should pay attention to more complex evaluations:

# Simple cases are not confusing
false || true # true
false or true # true

# This is more complex
a = false || true # a=true
a = false or true # a=false

# Also similarly as 1 + 2*3 returns 7, the following returns true:
false or false||true # true
# BUT! THIS IS IMPORTANT!
a = false or false||true   # a=false
a = (false or false||true) # a=true

Here is a list of operators precedence.

So the real difference will be noticed if you use the expression that includes any of the following operators:

  • .. ... - Range (inclusive and exclusive)
  • ? : - Ternary if-then-else
  • = %= { /= -= += |= &= >>= <<= *= &&= ||= **= - Assignment
  • defined? - Check if specified symbol defined
  • not - Logical negation
  • and - Logical composition

there might be others too.

You can thing about the difference between those as different between + and *: ||==* and or=+. The same applies to and and not.

You should really pay attention to that.

Personally I prefer || operator as its semantics is well understood and avoid or.

While it 'feels' like or is more friendly in many cases (see my code sample), even in trivial ones, it is a source of bugs.

Dmytrii Nagirniak
  • 23,696
  • 13
  • 75
  • 130
  • Your answer is much better than mine! I would add for the non-rubyists (and non-perl..ers?) that `or` is primarily used in situations like: `x = get_some_string or ""`. In this situation `get_some_string` could return nil or other 'falsy' value and x would become the empty string. Like Dmitriy says, though, `||` works perfectly well here too, it's just a convention you'll see occasionally in the ruby world (probably this is a carry-over from perl). – rfunduk Mar 04 '10 at 02:05
  • @thenduks: "x would become the empty string" No, it wouldn't. If you do `x = nil or ""`, x will be `nil`, not `""`. `""` will simply be the returned value of the expression. – sepp2k Mar 04 '10 at 12:23
  • Hmm quite right, `=` binds tighter than `or` does... My example was a poor one, then. – rfunduk Mar 04 '10 at 16:36
4

They do the same thing, but or has a lower precedence than || (and, in fact, or has a lower precedence than most of the other operators).

mipadi
  • 398,885
  • 90
  • 523
  • 479
  • they don't do the same thing because the same block of code will render a different output. – thenengah Mar 04 '10 at 01:48
  • What do you mean by lower precedence? – thenengah Mar 04 '10 at 01:49
  • 1
    Precedence defines order of operations. For example, `*` has a higher precedence than `+`, so the expression `1 + 2 * 3` returns `7` because the multiplication is done first. – mipadi Mar 04 '10 at 02:00
1

Your conclusions about the examples are incorrect.

if true || true || false
  puts 'something'
else
  puts 'nothing'
end

This code outputs 'something' because that's how the 'logical or' operation works. It reads like "if at least one of condition1 or condition2 or condition3 are true...".

The second example is exactly the same, but only because of the rules of precedence in Ruby.

rfunduk
  • 30,053
  • 5
  • 59
  • 54