POSTS

Revisiting LatinVerb and LatinIRB

Blog

About a year ago I went on a pretty aggressive tour talking about Metaprogramming in Ruby and speaking at Lone Star Ruby Conference and RubyConf 2011. Most of my observations as presented in my talk were based on working to build libraries for the conjugation and display of Latin verbs.

A year later, much has changed.

I left Cisco, made some forays into consulting, joined a startup and started writing a lot of Javascript and Rails. I was wondering, what changes would I make to LatinVerb knowing what I know now, one year later? The results of that line of inquiry are now available to see as of the latest commit (339125f976).

Preferring Composition

The chief change that I see is a move to use composition more readily. I think that I noticed that certain questions should be handled by objects which excel at answering a specific type of question. Many of these changes can be seen in LatinVerb.initialize. Instead of running a private method _init_by_string I create a LatinVerbInputSanitizer and pass the unsanitized data off to it.

I think another way of seeing this lesson is: don’t be afraid of lots of little classes.

Preferring Explicitness

I think one of the things that’s so amazing about Ruby and, especially, its metaprogramming facilities it that it makes it seem OK to be laconic or implicit in your commands. The last several months of work at shopittome.com have convinced me that explicitness is your friend and all the more so when you are working in a team. While you may be sure that some side-effect happens as a result of some expression, when you’re busy and want to make sure you’re not introducing a bug in a refactor, it pays to know what your team-mate or your past-self explicitly meant.

Use Parentheses

When you get a result back from a call, use parentheses to hold the argument list.

Metaprogramming-Fu

Recently James Edward Gray II pointed me to a post by Mike Burns which explains an amazing feature of Ruby’s respond_to? method. Basically if Ruby can’t find a method by the name which it is asked to respond_to?, it will execute respond_to_missing? as a last-ditch attempt to see if it should respect some method generated by playing with method_missing. It’s an astounding feature and Mike’s documentation and James’ pointer were real inspirations as I undertook this rewrite.

The file latinverb/metaprogramming.rb got much more intelligible after I moved to using this method.