"Morris, Chris" <ChrisM / SNELLINGCORP.COM> writes:

> In Delphi, my native tongue, a rule I follow is, "whoever creates an object
> is responsible for freeing it."

A fine rule!

> So, on to Ruby. This whole issue is not a problem due to garbage collection
> .. so which way would be the "Right Way" to do this? I'm leaning towards
> the original design:

Even with garbage collection, there are still resources that you need
to manage this way.

There are many options:

1. Encapsulate processing within a single method, just like your
   Delphi example.

2. Write a class, and encapsulate processing within it. This gives you
   a bit more flexibility, but you need to remember to free the
   resource at the end.

3. Use blocks. This is a wonderful Ruby trick for many kinds of
   resource. For example

   File.open("fred") do |aFile|
     str1 = aFile.gets
     str2 = aFile.gets
   end

   Here, 'aFile' will be set of an opened file object that is
   guaranteed to be closed when the block ends (and even if the block
   throws an exception).

   You can do this with your own resources:


   def createResource(params)
     res = Resource.new(params)

     begin
       yield res
     ensure
       res.free
     end
   end


   createResource(...) do |res|
     use res...
     use res...
   end


Regards


Dave