On Mon, 8 Oct 2007 07:40:42 +0900, Peter Bailey wrote:

> Now, I have to admit, I really need to look at your code, Jay, because, 
> to me, it's like a different "dialect."

Me too!  I've only been doing Ruby for a few years, and then only for play
- just started being a pro-fessional this summer.  Luckily, Ruby's so
natural, you'll be giving newsgroup advice yourself in just a few months.

The "block trick" happens to be one of the first Ruby idioms I read about.
In C++, people use RAII, Resource Acquisition Is Initialization, where you
wrap a resource in an object, "open" the resource by creating the object,
and "close" it by destroying the object.*

In Ruby, though, the object gets destroyed by garbage collection, not by
explicit destruction or stack unwinding.  So RAII wouldn't work; you can
never guarantee when (or if) the resource will be closed.  Instead,
resource-wrappers like File often let you specify a block; the wrapper
opens the resource, yields to the block, and closes the resource for you.
It's just a different wrapping idiom.  If you haven't discovered
http://gotapi.com already, load up the Ruby tab and check out IO#open.


* Wasn't RAII originally called RAIN, Resource Acquisition Is
iNitialization?  I could swear it was, and it certainly seems a more
memorable choice for an acronym, but Google's not bearing me out on this.
Then again, that was pre-Google.  Anyone?

-- 
Jay Levitt                |
Boston, MA                | My character doesn't like it when they
Faster: jay at jay dot fm | cry or shout or hit.
http://www.jay.fm         | - Kristoffer