From: Eero Saynatkari Date: 2006-12-14T07:20:33+09:00 Subject: Re: Pointers and References? --2PtegAy3ko4/l3FE Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On 2006.12.14 05:20, dblack@wobblini.net wrote: > Hi -- >=20 > On Thu, 14 Dec 2006, Christopher Coleman-Smith wrote: >=20 > >Simple question, i come from a C/++ background and i'm just really=20 > >starting to get "real things done with ruby" ( that should be a book=20 > >title). Anyway as i start writing methods that return objects and=20 > >variables and take objects and variables i've started to wonder how they= =20 > >actually get passed around. I haven't found a specific pointer or=20 > >reference type construction, although since everything i've tried to do= =20 > >has worked i've not tried very hard. > > > >I'm guessing that everything gets passed around by reference? Is that=20 > >true? Also what about situations where you'd really want to use a pointe= r,=20 > >like referring to huge things or ,god forbid, doing grimy things with=20 > >arrays. It is very nice having all these things automated, like the=20 > >garbage collection etc, but i still find myself wanting to free things.= =20 > >I've only just gotten over wanting to semi-colon every statement. >=20 > Basically, everything (or almost everything) is a reference: >=20 > a =3D [1,2,3] # a is assigned a reference to the array > b =3D a # b gets a copy of that reference > b.push(4) # the array now has four elements > p a # as you'll see if you print it via any reference >=20 > When you pass objects around via variables, technically you're passing > by value... but the "value" of the variable is a reference to an > object. >=20 > When you pass objects around via literals (like puts "hello"), a > reference to the literal is sent. >=20 > All of this means that if you do something to the arguments in a > method, the changes are permanent in the original object: >=20 > def add_element(array) > array.push("new element!") > end >=20 > a =3D [1,2,3] > add_element(a) > p a Just to make it clearer: anything destructive or mutating you do to the object inside the method will propagate to the original object. One perhaps surprising aspect of the 'everything is a reference value' style of argument passing is that assignment to the variable name will simply have it point to a different object rather than modify the original: a =3D "Hello!" def foo(string) string =3D "Hello to you too!" end =20 foo a p a # =3D> "Hello!" > So you'll see a fair amount of "dup"ing going on in methods that want > to be safe. >=20 > There's no reference-to-a-reference. It's not like C pointers, which > can point to other pointers. Every Ruby reference is exactly one step > away from the object it refers to, and all references to an object are > equivalent to all other references to that object. >=20 > Some values are immediate; i.e., when you assign a variable to them, > they get stored directly and not via a reference: integers, true, > false, nil, and symbols (and any I've forgotten). Hence the "almost > everything" above. I think this can just be considered to be an implementation detail, frankly. Fixnums etc. just do not have any destructive methods that one could employ inside a method. --2PtegAy3ko4/l3FE Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.5 (FreeBSD) iD8DBQFFgHwG7Nh7RM4TrhIRAt8iAJ49ufGEwMoYkV4ejdLdxrpoNIn9zACgs7Mt CpufGyLepP0QnYN6oxiTKd4= =PIBE -----END PGP SIGNATURE----- --2PtegAy3ko4/l3FE--