hoodwink.d enhanced


Sam Ruby Funnels Down Rails #

by why in inspect

Since David’s gone all this week, I’ll pick up some of the Rails news, how about? Especially when the Rails news gets us back into hacking plain old Ruby code. As is the case with Sam Ruby, who’s tinkering with Rails outside of the framework—he calls it his Rails Confidence Builder.

The script is sort of a stream-of-consciousness walk through ActiveRecord insertion and retrieval. Within the framework, the parts of this script get placed in different subdirectories, to ensure the MVC parts stay organized. I’ve annotated Sam’s example with brief comments indicating where each piece would go in a Rails app. Keep in mind that the db/entries.sql would be described in plain SQL and the config/database.yml would be described in YAML.

Sam uses Ruby’s pipe opener to communicate with MySQL and notes how the closure neatly tucks the ends for the resource.

While IO.popen can be used as a traditional function, returning a stream that you can write to and ultimately are responsible for closing, I’m making use of another idiom: passing a block to the function. IO.popen will bind the file handle to the argument you specify, and take care of ensuring that the pipe is closed properly when you exit the scope of the block. Sweet!

Yeah, I’m so glad I don’t have to figure out where to put IO.close ever again. (Seen on intertwingly.)

said on 13 Aug 2005 at 02:47

What a great sample!

Comments are closed for this entry.