From: Eric Mahurin Date: 2008-06-06T00:35:28+09:00 Subject: Re: A crosspost from the Perl Community On Thu, Jun 5, 2008 at 10:00 AM, Dave Bass wrote: > You can get bitten by this. Everything is call-by-reference, not > call-by-value as in Perl, C etc. Change a function's parameter and you > change the original, not a local copy of it. No, ruby is still call-by-value. The "value" is an object reference (or simply an object in ruby terms). If the object is mutable, then a function can modify it. Call-by-reference on the other hand refers to passing an lvalue reference. If ruby had call-by-reference, you could pass an lvalue (something that can be on the left side of an assignment) and when the function modified the corresponding argument variable, the lvalue would change (not just the object that the lvalue has). Perl actually does have call by reference. The items in @_ for a sub can be lvalue references and changing them changes the callers lvalue. But, most start a perl sub with something like this copies that values (losing the references): my($a, $b) = @_; C++ can also call-by-reference and C can emulate it (with pointers to lvalues). With a bit of work you can also emulate call-by-reference in Ruby. Here's an example using simple lambdas: def swap(get0, set0, get1, set1) tmp = get0 set0[get1] set1[tmp] end a = 1 b = 2 swap(lambda{a}, lambda{|v| a=v}, lambda{b}, lambda{|v| b=v})