Difference Between "Or" and || in Ruby

Difference between or and || in Ruby?

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.

Difference between .. and ... in Ruby

In ruby 1...5 gives you a range which doesn't include 5
whereas 1..5 gives you a range which does include 5

eg:

>> (1..5).to_a
[
[0] 1,
[1] 2,
[2] 3,
[3] 4,
[4] 5
]
>> (1...5).to_a
[
[0] 1,
[1] 2,
[2] 3,
[3] 4
]

What's the difference between or and | in ruby?

The | operator is a binary mathematical operator, that is it does a binary OR and works on a numerical level:

1 | 2
# => 3
4 | 3
# => 7
1 | 2 | 3
# => 3

This is because it's manipulating individual values as if they were binary:

0b01 | 0b10
# => 3 (0b11)

The || operator is a logical one, that is it returns the first value that's logically true. In Ruby only literal nil and false values evaluates as logically false, everything else, including 0, empty strings and arrays, is true.

So:

1 || 2
# => 1
0 || 1
# => 0

The or operator works almost exactly the same as || except it's at a much lower precedence. That means other operators are evaluated first which can lead to some problems if you're not anticipating this:

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

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

This is because it's actually interpreted as:

(a = false) or true

This is because = has a higher precedence when being evaluated.

What is the difference between += and =+ in Ruby?

  1. a += b is syntactic shorthand for a = a + b
  2. a =+ b is similar to a = + b

So, firstValue = firstValue + secondValue is the same as firstValue = firstValue.+(secondValue) in ruby.
firstValue + = secondValue – increment Add the value of secondValue to the value of firstValue , store the result in firstValue, and return the new value.

what is the difference between += and =+ in ruby?

There's no such token as =+; it's actually two tokens: assignment followed by the unary + operator; the latter is essentially a no-op, so @@num_things =+ 1 is equivalent to @@num_things = 1.

Since there is a += token, the language parser will parse it as a single token.

(In the early formulations of BCPL which was the precursor to C, the modern -= operator was written as =-.)

What is the difference between @@ and @ in Ruby?

A variable prefixed with @@ is a class variable and one prefixed with @ is an instance variable. A great description can be found in this answer: https://stackoverflow.com/a/5890199/1181886

Difference between and and && in Ruby?

and is the same as && but with lower precedence. They both use short-circuit evaluation.

WARNING: and even has lower precedence than = so you'll usually want to avoid and. An example when and should be used can be found in the Rails Guide under "Avoiding Double Render Errors".

Ruby: what is the difference between the comparatives: || and or

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.

What is the difference between :to and = in rails

In context of Rails routes:

  • Is there a difference between these two statements?

There is no difference.

  • If so why is one better than the other?

No, it's the same.

  • Why is the rails community switching to the ":" notation (or are
    they)?

Just a more readable, 'from' => 'to' and 'from', to: 'to'

  • Moving forward with rails 4 and soon 5, are both formats still
    acceptable?

Yes.

The => notation it's a hash ruby feature, and related to the :symbol.
You can write symbols by two ways :key => value and key: value.



Related Topics



Leave a reply



Submit