The calling gsub with a block and shifting off of the bound values inside the block is so cute that I want to give it a hug. You know, I cut my teeth doing functional programming and I usually have this aversion to such a side-effect-heavy style. But treating gsub as an iterator — and using shift as a kind of index into the iteration — is so elegant you have to love it. It makes you remember that sometimes things are beautiful because they are imperative, not despite.

Of course, you could write this purely:

statement.gsub('?', '%%s') % values.map { |v| quote_bound_value(v) }

(I’m showing off here a bit with the % (percent) operator on String.) So this latter code is pure and it’s clear. It may even be more terse than the imperative version since it lacks the duping of the values array. But I don’t know about you but I don’t want to give this functional code a hug. The imperative version is like a baby panda bear:

Don’t you want to hug that? Whereas the Functional Version is like kind of like a Japanese carp.

If there’s any lesson from this it’s: know which standard library methods take a block.

Here is some code I hacked up recently somewhat in the spirit of the gsub-with-block approach above:

Don’t ask what this code does, it’s not what you think (this is operator overloading taken to its psychedelic conclusion). But note the use of min taking a block. This is the same as providing a custom ordering relation to sort, a pretty common idiom in many languages. But here I’m not interested in sorting attributes based on their intrinsic hashrocketness; rather, i need to sort them based on a comparison to some other object. min takes a block gsub takes a block sort takes a block… you’d be surprised at what takes a block. Look it up.

Now, I know some of you are going to say, “I did look it up, and that’s
not true.” That’s ’cause you looked it up in a book. Next time, look it
up in your gut.
— Stephen Colbert

Interesting Things

Erector is a Builder-like view framework, inspired by Markaby but overcoming some of its flaws. In Erector all views are objects, not template files, which allows the full power of object-oriented programming (inheritance, modular decomposition, encapsulation) in views.

Treetop is a language for describing languages. Combining the elegance of Ruby with cutting-edge parsing expression grammars, it helps you analyze syntax with revolutionarily ease.

Ask for Help

“Can anyone suggest any solutions, or point to a web site that seems to have solved the following problem: an iframe that’s showing content from one domain wants to communicate with its parent document that’s showing content from another domain, without modifying the parent document’s URL or refreshing the parent document’s page? Adding Javascript to the parent document is okay, but installing a proxy on the server of the parent document’s domain isn’t possible.”

Over the next week or so I’ll be sharing Ruby idioms and flourishes that I quite like. Today I’d I’ll show a few tiny uses of splat! that make me tremble with delight.

Splat! – For Beginners

Splat! is the star (*) operator, typically used in Ruby for defining methods that take an unlimited number of arguments:

def sprintf(string, *args)
end

It can also be used to convert an array to the multiple-argument form when invoking a function:

some_ints = [1,2,3]
sprintf("%%i %%i %%i", *some_ints)

Splat! – For Wizards

Array to Hash Conversion

The best use of splat! for invoking a infinite-arity functions I’ve ever seen is the recipe for converting an array to a hash. Suppose you have an array of pairs:

array = [[key_1, value_1], [key_2, value_2], ... [key_n, value_n]]

You would like to produce from it the hash: {key1 => value1 ... } You could inject down the array, everybody loves inject, but there is a better way:

Hash[*array.flatten]

Amazing right? This relies on the the fact that the Hash class implements the [] (brackets) operator and behaves thusly:

Hash[key1, value1, ...] = { key1 => value1, ... }

Heads or tails?

Splat! can be used for more than just method definition and invocation. My personal favorite use is destructuring assignment. I read this in Active Record’s source code recently:

def sanitize_sql_array(ary)
statement, *values = ary
...
end

This is invoked when you do something like User.find(:all, :conditions => ['first_name = ? and last_name = ?', 'nick', 'kallen']). Splat! is used here is to get the head and tail of the conditions array. Of course, you could use always use shift, but the functional style used here is quite beautiful. Consider another example:

first, second, *rest = ary

One final trivium (#to_splat aka #to_ary)

You can actually customize the behavior of the splat operator. In Ruby 1.8, implement #to_ary and in 1.9 it’s #to_splat. For example

So I’m giving a talk at RailsConf, the last day, the last time slot before the keynote — Sunday @ 1:50

My talk is in the “advanced” track, and is intended for language nerds and Ruby programmers who already have some metaprogramming experience. The topic is “ACTIVERECORD ASSOCIATIONS AND THE PROXY PATTERN”. I will look at

several implementation techniques for the proxy pattern (aka Blank Slate),

and will look very closely at the implementation details and advanced features of ActiveRecord’s association proxies, Named Scope’s repository proxies, and several other cool uses of this pattern.

If you’re looking for an intense, detailed, close look at very advanced Ruby coding techniques then this talk is for you. The full outline is here

Today I was thinking aloud about Tree Regular Expressions and how they might make a nice query language for document databases like CouchDB. Someone pointed out that CSS3 selectors might make a great concrete syntax for this. One thing lead to another and I thought, why not build a relational database in HTML? So I did. I even got inner joins working.

You can download the source from Github. Please see the included spec (screwunit_spec.js) to get up and running.

Implementation Details

Screw.Unit is implemented using some fancy metaprogramming learned from the formidable Yehuda Katz. This allows the describe and it functions to not pollute the global namespace. Essentially, we take the source code of your test and wrap it in a with block which provides a new scope:

Furthermore, Screw.Unit is implemented using the Concrete Javascript style, which is made possible by the Effen plugin and jQuery. Concrete Javascript is an alternative to MVC. In Concrete Javascript, DOM objects serve as the model and view simultaneously. The DOM is constructed using semantic (and visual) markup, and behaviors are attached directly to DOM elements. For example,

Thanks to

For the last several months I’ve been producing a Shared Items feed in Google Reader that some of my friends enjoy. I subscribe to dozens of blogs, sift through a couple hundred items per day, and curate what I think is the highest quality content.