From: nainar Date: 2003-12-08T03:06:03+09:00 Subject: Re: newbie question On Sun, Dec 07, 2003 at 08:14:01PM +0900, T. Onoma wrote: > On Sunday 07 December 2003 10:41 am, nainar wrote: > > 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. > > I find it confusing sometimes myself too, but it does "pan out" when you think > about how ruby works: > > ��def f2(b) > � ��b[0]=[9,8,7] > ���end > > In this case b references the passed variable like a c pointer, so assignment > to one of its elements effects the original. > > �def f1(a) > �� a=[9,8,7] > ���end > > In this case you have "dettached" the original reference to a, giving it a new > one, so the original is unaffected. Try this instead: > > �def f1(a) > �� a.replace [9,8,7] > ���end > > This all has to do with what = does --it assigns reference, and is not an > inplace assignment. > > T. > > > > I am beginning to understand this: If you assign to the variable within the function the original is NOT modified. if you modify the variable within the function then the original is modified. It means if the variable is a simple type like a numeric it cannot be modified. If I try something like: v=99 def f1(n); n=n*n ; end f1(v) it will not work. Why can't we have something simpler/more explicit like in C or even Perl?. v.nainar