Gerald Vim wrote in post #1127142:
> On 13/11/2013 11:32, Carlo E. Prelz wrote:
>>    x = x + 1
>
> So a parameter is passed by reference but as soon as you modify it
> you've cloned the object?

No, not at all.

``x = x + 1'' breaks down like this:

1. construct an object which is an Integer whose value is 1
2. find the object referred to by the variable named "x"; in this case,
an Integer
3. invoke the method called "+" on that object, passing the newly-minted
Integer as the first/only parameter*
4. update the variable named "x" to refer to the result of that method
invocation (i.e. this is the "=" part)

*Note: the "+" method on an Integer is non-destructive, internally it
creates a new Integer object whose value is the sum of the receiver and
the parameter, and returns that.  Integer objects are immutable in ruby.

There are all sorts of other specifics going on (singleton Fixnums for
starters) but this simplification isn't inaccurate.  Assignment is an
operation on the *variable* (a reference), and method invocations
(including "+") are operations on the *object* referred to by the
variable.

---

Here is a counter-example, assuming y = "abc", because I like writing 
more than necessary:

  y = ( y << "d" )

1. construct an object which is a String whose value is "d"
2. find the object referred to by the variable named "y"; in this case,
our a String
3. invoke the method called "<<" on that object, passing the
newly-minted String as the first/only parameter
4. update the variable named "y" to refer to the result of that method
invocation

In this case the "<<" method on a String *is* destructive; it changes
the value of the Object, by appending the parameter to it.  Fortunately
for
us, it also returns the (now updated) receiver.  We could have left the
code as:

  y << "d"

... which would have saved us a variable assignment, but both snippets
result in an identical state.

-- 
Posted via http://www.ruby-forum.com/.