On 27.01.2007 01:43, dblack / wobblini.net wrote:
> Hi --
> 
> On Sat, 27 Jan 2007, Martin C. Martin wrote:
> 
>> Phrogz wrote:
>>> If you pass an immutable type by reference, does it make a sound?
>>> Er, I mean...
>>> If you passed an immutable type by reference, how would you know that
>>> it wasn't passed by value?
>>
>> Is there any way for the function you're calling to modify the value 
>> of the variable in the caller?  Pass by reference can do that.
> 
> You can modify the object to which the variable refers:
> 
>   def change_me(obj)
>     obj << "hi"
>   end
> 
>   arr = [1,2,3]
>   change_me(arr)
>   p arr          # [1, 2, 3, "hi"]
> 
> In this example, arr contains a reference to an array.  In change_me,
> obj contains another copy of the same reference, so you can use it to
> manipulate and change the original array.
> 
> I still wouldn't call this pass by reference (see my earlier post in
> this thread).

Absolutely right: Ruby uses pass by value - with references.

irb(main):004:0> def foo(x) x = 10 end
=> nil
irb(main):005:0> def bar; x = 20; foo(x); x end
=> nil
irb(main):006:0> bar
=> 20
irb(main):007:0>

There is no standard way (i.e. other than involving eval and 
metaprogramming magic) to make a variable in a calling scope point to 
another object.  And, btw, this is independent of the object that the 
variable refers to.  Immediate objects in Ruby seamlessly integrate with 
the rest (different like POD's in Java for example) and from a Ruby 
language perspective you don't see any difference (other than 
performance maybe).  This is one of the reasons why Ruby is so elegant.

Kind regards

	robert