Metaprogramming Ruby

There are thousands of books that will take you from illiterate to novice in any programming language. But finding those that will take you from novice or intermediate to expert is hard. I remember reading Effective Java some years ago and wishing I had something like that for Python. I’ve never found one.

Metaprogramming Ruby is a great book full of very interesting knowledge, full of those things that separate a Ruby programmer and an export Ruby programmer. Before finishing the book I’ve already put to use some of the lessons and it saved me a lot of time. The book payed for itself before I’ve finished reading and I really recommend it to anyone who is serious about coding in Ruby.

360 vision

I’m surprised I’ve never seen this idea being tried…

When I’ve was a kid I’ve read a report about an experiment. A guy put goggles with screens so that he could see nothing but the screens (ala virtual reality). The same device also had one or two cameras and the screens projected an upside-down image of the camera(s). After a while (I remember three days) of bumping into furniture, walls and eventually the floor, the guy stopped noticing the images were upside down. The brain switched to interpret the new images.

When they removed the goggles, suddenly everything was upside down and the brain took the same amount of time to switch back.

I’ve immediately got this idea: let’s put cameras all around the head in a helmet and let’s compress the 360 image into the two screens. At first we’ll be very confused but after a while we’ll be able to see 360. Wouldn’t it be great?

The magic of Bundler

Recently I reported a bug for Formtastic. Justin French, the author of Formtastic, created a branch and made a fix. He then asked me for my feedback.

I look at the code and then decided to give it a try. In a pre-Bundler world that would have required this:

  1. Find a directory to play with this.
  2. Clone the Formtastic repository with Git from
  3. Create a local branch tracking the remote branch with the fix, GH-264. This is something I don’t do often enough with Git and every time I have to look it up.
  4. Figure out how to build a gem out of it. Is it rake? is it rake build? is it rake gem? This might also fail and need fixing some stuff.
  5. Install said gem, which is not that trivial. Should I install as my user or as root? Should I remove the currently installed version of the gem? If the branch didn’t have an increase in version number it could be problematic.
  6. Test my application. Make sure it’s picking up the new gem.
  7. Uninstall the gem, maybe re-install the stock gem.
  8. Delete the temporary directories I’ve created to hold the cloned repository (this is something I always forget to do and a month later I’m wondering: what’s this? is there any important changes I’ve did in this repo?).
  9. The tasks are not that big, but are very inconvenient to do and uncomfortable for a perfectionist like me. Thankfully I’m using Bundler, so the above was like this:

  1. Add :git => "", :branch => "GH-264" to the Formtastic line in Gemfile.
  2. Run bundle install.
  3. Test app.
  4. Revert the Gemfile change.
  5. Run bundle install.
  6. I really love Bundler.

Redirecting back

It’s very common in Rails CRUD to have a create and update actions that redirect back to the show action. The idea is that you show an object, click edit, save, go back to showing said objects with your changes.

All is fine until you have an edit link somewhere else. Let’s say you have an edit link in the listing of the CRUD, when someone uses you have to go back to the listing, not the show.

Well, Ruby on Rails provides just the thing for that:

redirect_to :back

That will send you back wherever you came from. The problem with that is that it will raise an exception if there’s no HTTP_REFERER, so you’ll have to write something like this:

  redirect_to :back
rescue ActionController::RedirectBackError
  redirect_to somewhere_else

Of course there’s a pattern, so almost all my projects, at one time or another end up with this snippet of code in the application controller:

def redirect_back_or_to(*args)
  redirect_to :back
rescue ActionController::RedirectBackError
  redirect_to *args

I really like how every method is an implicit begin, it really looks beautiful. Then you just do:

redirect_back_or_to somewhere_else

I’m surprised Rails didn’t come with something like that out of the box, or maybe I just missed.

fofof was useless

It’s always hard to kill your own code, but not killing it when you have to is worst in the long run. My idea for fof and consequently my gem fofof was useless.

First I’ve discovered it didn’t work at all with the new Rails 3 query syntax. When I started to find a fix I’ve discovered I could replace the whole thing with:

 || raise(

The examples in the Find or 404 post would end up like:

Blog.find_by_id(id) || raise(


blog = Blog.fof.find(blog_id)
post = blog.posts.find_by_id(id) || raise(

It’s less code, it’s more robust, I even think it’s much more readable. So there you, I’m killing fofof.

Sharing my code

I’ve recently wrote several posts that contiained code to copy and paste:

I don’t like copying and pasting code and since I was already doing it between several of my projects, I took those pieces of code and package them as gems. If you want, you can use them too:

I find it really awesome how many times some of my gems were downloaded:

Update: fofof is actually useless.