From: nainar Date: 2003-12-07T18:41:26+09:00 Subject: Re: newbie question On Sun, Dec 07, 2003 at 03:14:28PM +0900, Dmitry V. Sabanin wrote: > On Sunday 07 December 2003 12:43, nainar wrote: > > If function are params are only copies , why do the two functions below > > behave differently: > > > > def f1(a) > > a=[9,8,7] > > end > Because in this case, not object that 'a' refers to is changed, but variable 'a' itself assigned to a new object [9,8,7]. > > def f2(b) > > b[0]=[9,8,7] > > end > But now, object that 'b' refers to is changed, not variable 'b'. > > > v=[1,2,3] > > f1(v) > > p v # prints [1,2,3] : 'v' not changed > When variable assigned to a new object, old object still exists. And v holds reference to it, no matter what f1 did. > > > f2(v) > > p v # prints [[9,8,7],2,3] ! > > -- > sdmitry -=- Dmitry V. Sabanin > MuraveyLabs. > > > Thanks for prompt reply. But why is a treated differentlyi from b[0] ? . In C, parameters are passed by value , unless pointers to variables are used . In Perl, params are passed in the '@_' array and contains references to the original variables and assignments to the elements of @_ are reflected in the original vars(Execept when the whole '@_' is assigned to at once ). How does it work in Ruby?. I couldn't find anything on this in docs. v.nainar