On 12/13/06, Christopher Coleman-Smith <chris.colemansmith / gmail.com> wrote:
> Simple question, i come from a C/++ background and i'm just really
> starting to get "real things done with ruby" ( that should be a book
> title). Anyway as i start writing methods that return objects and
> variables and take objects and variables i've started to wonder how they
> actually get passed around. I haven't found a specific pointer or
> reference type construction, although since everything i've tried to do
> has worked i've not tried very hard.
>
> I'm guessing that everything gets passed around by reference? Is that
> true? Also what about situations where you'd really want to use a
> pointer, like referring to huge things or ,god forbid, doing grimy
> things with arrays. It is very nice having all these things automated,
> like the garbage collection etc, but i still find myself wanting to free
> things. I've only just gotten over wanting to semi-colon every statement.
>
> Probably by asking this i'm showing how i really haven't groked ruby at
> all. I'm happy to admit that.
>

Everything is being passed by reference, but the references themselves
are 'hidden', to enable GC.
data = File.read('ridiculously large file')
some_object.some_method(data)

The data is not copied; some_method just receives a reference, and a
binding (the String object containing the data is bound to the name
'data').

Calling methods on 'data' implicitly dereferences it (this isn't
really how it's referred to in Ruby, but it seems to be how C people
like to think of it. heh.)

All you need to do to 'free' memory is keep your methods short.  Local
variables will go out of scope, and be collected for you.

By the way.. the "real things done with ruby" book is called 'The Ruby
Way - 2nd Edition', by Hal Fulton. =)