Saturday, February 9, 2008

It's Not Meta, It's Just Programming

Here we have an RSpec set of examples for a Rails plugin which modifies ActiveResource.

I've complained about the complexity of this plugin - check the link for context - but really it's a testament to Ruby's flexibility. You can pretty much redesign ActiveResource's entire approach to HTTP connection in 137 lines of code over the course of a couple days.

Brag brag brag. That's not the point. The point is, this bit here.

This is one of the less frequently-used idioms in Ruby, but it's very handy. With it you can add methods to any arbitrary object.

Typically, the example use cases you see for "metaprogramming" are often complex and involved. My use case here was very simple: my spec wouldn't run because I didn't have a particular method on my mock; so I added the method to the mock. Boom. Done. Nothing clever, nothing deep. You need the method? Add the method. It's that simple.

Metaprogramming isn't really such a big deal.