From: dblack@... Date: 2007-01-28T13:08:50+09:00 Subject: Re: pass by reference? Hi -- On Sun, 28 Jan 2007, Thomas Hafner wrote: > Xavier Noria wrote/schrieb <73286BF0-AFE1-40C1-9704-16A3E129CD56@hashref.com>: > >> In C++ since the copy constructor is involved, I found people in >> freenode#c++ consider &-arguments to be pass-by-reference because >> they can modify the objects in the caller, albeit the assigment test >> discussed in the thread does not work, that'd be >> >> def m(b) >> b = 7 >> end >> >> a = 3 >> m(a) >> # a expected to be 7 in pass-by-reference > > That's another story. With the code above you rather reset b to a new > object instead of modifying the passed object. Look here: > > def m(b) > b.shift > b.unshift(7) > end > a = [3] > m(a) > => [7] > > Would that be the same with pass-by-value? No, I don't think so. It depends what value you're passing. If the value is an array, and it gets copied into b, then it wouldn't work that way; but what's happening is that a *reference* to an array is getting passed by value. That value (which is a reference to an array) is assigned to the variable a in the caller's scope, and also to the variable b inside the method. David -- Q. What is THE Ruby book for Rails developers? A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black) (See what readers are saying! http://www.rubypal.com/r4rrevs.pdf) Q. Where can I get Ruby/Rails on-site training, consulting, coaching? A. Ruby Power and Light, LLC (http://www.rubypal.com)