From: daz Date: 2005-11-12T11:57:14+09:00 Subject: Re: Function Variable and Return Value References Eric Hofreiter wrote: > The question is, how exactly does ruby behave in this respect? > I haven't quite figured out what the rules are. FAQ [4.5: How are arguments passed?] http://www.rubygarden.org/faq/entry/show/27 > It appears that Fixnum and basically every user-made object are > always passed by value, and can not reference each other. The rules of assignment apply (ref FAQ). > > However, arrays seem to be passed into functions by reference, > and returned by value, though the objects they contain are copied. That's a bit of a muddle (if I might be so bold ;). > > At first, I thought variables were always pointers, and things > such as a = b meant that if you change a, you change b. a and b now reference the same object -- that's right. > It seems now that this is in fact never the case. Oh, no :) > So that brings me to my second question: what are the purpose of > the methods dup and clone? I thought they would be useful if > you needed to assign copies rather references, but it seems that > objects are copied by default when assigned. There's no copying on assignment - that's the same reason you may need dup/clone. I can usually manage to get most of the way through a prog without resorting to dup/clone by using the feature that many methods return a modified object (a modified copy of the target). > > Can someone please explain this to me? I'm not very chatty but the above might get you started. daz