E
Eric Hofreiter
--0-2040383402-1131761513=:99866
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable
In C++, one thing that is certain is that all variables passed into funct=
ions and all return values are actually copies. Therefore, the functions=
can't manipulate their arguments directly. As everyone knows, the easie=
st way to get this effect is to pass in by reference. This way a copy is=
not made; the actual variable is passed in, allowing direct modification=
A similar idea can be used with return values or even without a functi=
on at all.
=20
The question is, how exactly does ruby behave in this respect? I haven't=
quite figured out what the rules are. It appears that Fixnum and basica=
lly every user-made object are always passed by value, and can not refere=
nce each other. However, arrays seem to be passed into functions by refe=
rence, and returned by value, though the objects they contain are copied.
=20
At first, I thought variables were always pointers, and things such as a =
=3D b meant that if you change a, you change b. It seems now that this i=
s in fact never the case. So that brings me to my second question: what =
are the purpose of the methods dup and clone? I thought they would be us=
eful if you needed to assign copies rather references, but it seems that =
objects are copied by default when assigned. Can someone please explain =
this to me?
=09
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable
In C++, one thing that is certain is that all variables passed into funct=
ions and all return values are actually copies. Therefore, the functions=
can't manipulate their arguments directly. As everyone knows, the easie=
st way to get this effect is to pass in by reference. This way a copy is=
not made; the actual variable is passed in, allowing direct modification=
A similar idea can be used with return values or even without a functi=
on at all.
=20
The question is, how exactly does ruby behave in this respect? I haven't=
quite figured out what the rules are. It appears that Fixnum and basica=
lly every user-made object are always passed by value, and can not refere=
nce each other. However, arrays seem to be passed into functions by refe=
rence, and returned by value, though the objects they contain are copied.
=20
At first, I thought variables were always pointers, and things such as a =
=3D b meant that if you change a, you change b. It seems now that this i=
s in fact never the case. So that brings me to my second question: what =
are the purpose of the methods dup and clone? I thought they would be us=
eful if you needed to assign copies rather references, but it seems that =
objects are copied by default when assigned. Can someone please explain =
this to me?
=09