activerecords and closures and ruby, oh my

stumbled onto ruby on rails the other day–but i can’t retrace how (yet). which led me to some o’reilly article on ruby on rails that like everybody (err, every programmer) in the blogosphere seems to be simultaneously linking to. and i came away thinking “ruby on rails is not for me.” at least not for the financial web application that is my job. but then i tend to have a gut negative reaction to most things the first time i experience them. i’m a bit of a skeptic that way.

so i googled ruby tonight and read up a bit on its features, one of which is the idea of closures (lexical closures) which i read about in hackers and painters, and didn’t understand then. so i googled “lexical closure” and via the newly discovered, right-up-my-alley blog, StronglyTyped, I learned that a lexical closure is something like a rudimentary/proto object. i still don’t know why it’s called a lexical closure.

so i installed sage cause paul keeps mentioning it, and because i don’t have/use a newsreader/aggregator, and because my life is probably incomplete as a result, missing out on all that information and such.

of course StronglyTyped’s RSS feed isn’t well-formed because there’s a less-than sign in some ruby code he posted (ruby uses less-than signs where java/php uses the keyword extends). and this error just happens to be in a post about ActiveRecord, which appears to be a major part of Ruby on Rails, and which is a design pattern i just happened to be reading about in advanced php programming at the end of last week!


it should be noted that the very neat app basecamp was developed using ruby on rails by the rails developer ( also who also wrote my favorite wiki instiki… sorry i think the guy is really cool :) oh, and now he works for 37signals

hmm… text justification makes all that ranting even more odd

i linkified the urls

man, check out the c2 article linked from StronglyTyped on Lexical Closures for a veeery exhasutive discussion (and thanks for the links)

ha! the c2 article is actually where i started (it’s like the second hit in google), but it was too dense for me, which is how i found strongly typed.

corey thanks. i’ll take a look.

It’s called a “lexical closure” because it “lexically closes” an anonymous function (unnamed subroutine, lambda, whatever). A lexical closure is an anonymous function returned from another function (and generally stuffed into a variable for reuse). Within it, the thing operates on its closure variable. The closure variable is a variable that is created within the scope of the enclosing (generally non-anonymous) function. When that enclosing function goes out of scope, there’s no longer any way to directly access the variable that was created in it — but the variable is not “destroyed” because it also exists within the anonymous function that was returned.

For example:

function {
foo = 3
return { foo ++; print variable}

bar = function()

The reason it’s called a lexical closure, then, is the use of lexical scope for that variable (in this case, “foo”): lexical scope makes foo go out of scope after the return, but its value is still tracked via the anonymous block of code that is stuffed into bar when the enclosing function returns it.

I don’t know if that helps explain the situation any, but that’s sort of a rough explanation of what’s going on. My familiarity with lexical closures is mostly via Perl, which makes use of reference incrementing and decrementing along with lexical scoping for lexical closures.


Email (optional)

Blog (optional)