1

So I tried searching with different keywords but either I'm a bad search-operator or It's too simple of a problem. Either way my problem is that I can't get my head around this logic in Ruby.

x = 5
x = x + 1

So if I understood it correctly x becomes 6. Why is that? if you "reassign" the value literally doesn't it become "x + 1" without having anything to do with the first line.

Thanks.

Finedd
  • 13
  • 4
  • 2
    The right side is evaluated first, i.e. `x + 1` => `5 + 1` => `6`. The result (6) is then assigned to `x` – Stefan Feb 28 '15 at 08:14

2 Answers2

1

Operators are applied in order of their precedence.

It's not that the right side is (always) evaluated first, it's that addition has higher precedence than assignment. Run irb to test.

$ irb
2.2.0 :001 > x      # => the variable 'x' doesn't yet exist.
NameError: undefined local variable or method `x' for main:Object
    from (irb):1
    from /home/mike/.rvm/rubies/ruby-2.2.0/bin/irb:11:in `'
2.2.0 :002 > x = 5  # => Assign x the value 5.
 => 5               # => Evaluating the expression 'x = 5' returns 5
2.2.0 :003 > x      # => and the value of 'x' is 5.
 => 5 
2.2.0 :004 > x = x + 1  # => Addition has higher precedence than 
                        # => assignment. Ruby evaluates 'x + 1', then
                        # => assigns the result to 'x', and finally 
                        # => returns the result.
 => 6 
2.2.0 :005 > x          # => 'x' has the same value as the previous 
                        # => result.
 => 6 
2.2.0 :006 > x + 1      # => This expression returns the value 7.
 => 7 
2.2.0 :007 > x          # => But without the assignment operator (=),
 => 6                   # => the value of 'x' didn't change.

Why is this important? Because operator precedence doesn't always work the way you think it ought to.

$ irb
2.2.0 :001 > true and false            # => That makes sense.
 => false 
2.2.0 :002 > x = true and false        # => That *seems* to make sense, but
 => false 
2.2.0 :003 > x                         # => 'x' has the value true, because
 => true                               # => assignment has higher 
                                       # => precedence than Boolean 'and'.
2.2.0 :004 > x = (true and false)
 => false 
2.2.0 :005 > x
 => false 
2.2.0 :006 > 

Most people expect the expression x = true and false to be equivalent to x = (true and false), because they expect Ruby to always evaluate the right side first. But Ruby doesn't do that. It evaluates assignment (=) before Boolean and. So the expression x = true and false is actually equivalent to (x = true) and false.

Ruby's precedence table on SO

Community
  • 1
  • 1
Mike Sherrill 'Cat Recall'
  • 91,602
  • 17
  • 122
  • 185
0

The right side is evaluated first

Ruby itself will give you an object_id for each of them and you could possibly use this to identify all your objects. But there’s a little gotcha:

x = 'matz'
=> "matz"
y = 'matz'
=> "matz"
[ x.object_id, y.object_id ]
=> [2164843460, 2134818480]

So Ruby interpreter identify the object_id and assign value in that memory place

interpreter starts with the operator having the highest precedence

user3118220
  • 1,438
  • 12
  • 16
  • Well that explains it a lot further, thank you! For what reason does the interpreter start from the right? – Finedd Feb 28 '15 at 10:17
  • The interpreter starts with the operator having the highest precedence. Addition has higher precedence than assignment. See [this SO community wiki](http://stackoverflow.com/a/21060235/562459). – Mike Sherrill 'Cat Recall' Feb 28 '15 at 11:23