From: Eric Mahurin Date: 2007-10-03T04:37:28+09:00 Subject: Re: Reference vs. Reference On 10/1/07, Robert Klemme wrote: > On 01.10.2007 22:07, Robert Dober wrote: > >> although > >> the description at > >> http://en.wikipedia.org/wiki/Call_by_reference#Call_by_reference does > >> not cover Ruby properly - unless you interpret "temporary object" as > >> "copy of a reference" because this is how it works. > > Maybe call by copy is the exact term; what you think? > > I have never heard this term before; I would guess it's not a standard > CS term. One could argue that Ruby uses something like "call by copy of > reference" because that's basically what happens. But I haven't seen > this term either; I don't think it is standard. So I'd say it's "call > by reference" because that describes best what happens, especially that > callers see manipulations of objects. And the fact that Ruby does not > have the aliasing effects that C++ references exhibit does not > disqualify the term "call by reference" for Ruby. > > Behavior wise it's the same as passing a pointer to a C function. You > can change the pointer in the function without affecting the pointer in > the caller. Yet both refer on function invocation to the same value. I agree with Robert Dober. What ruby does is not call-by-reference. Every other place I've seen this phrase used it means that when you pass an lvalue to the function and the function modifies the corresponding argument, it modifies that lvalue. The "reference" in call-by-reference is a reference to an lvalue, not an object. C++ isn't the only language that has this concept. In C, you can emulate this concept easily by passing the address of an lvalue and the function dereferencing that pointer to modify the lvalue. There is no easy built-in way to do this in Ruby. The solution is typically to return the new value and have the caller explicitly assign the lvalue. I'm not saying that the functionality that Ruby has is a major issue (although there have been occasions where I've wanted a pointer concept), but saying that it does call-by-reference is just not true. The calling mechanism in Ruby seems closest to passing pointers to values in C, where all variables also contain pointers to values. I think of all ruby variables as object pointers and it makes sense to me. If I needed to put a name to calling mechanism (which I don't), I guess I'd say call-by-pointer. The pointer doesn't necessarily have to be a memory address though. Or you might say that ruby's variables are all object pointers and ruby does call by value. Eric