Picture of stu

Write for Others as for Yourself

  • Posted By Stuart Halloway on January 31, 2007
Don's post about the Sellsian Approach is right on. This dovetails nicely with what I love most about Ruby: the ease with which you can give your fellow programmers a domain specific language instead of an API. Last week I found myself leaving a comment "for the next programmer" that looked like this:
# foo must be a Hash of String to Widgets 
My gag reflex took over, and so a few minutes later I committed the following executable Ruby instead:
invariant 'foo is a Hash of String to Widgets'
With regular expressions, instance_eval, and threequals, it was trivial to make invariant into a fairly general mechanism.
Comments
  1. MatJanuary 31, 2007 @ 05:15 PM
    I like the technique. It brings to mind what ruby-contract does with method signatures. I'm not fond of the string parsing though. What about this? invariant :foo, Hash, String => Widget Or perhaps a monkey patch to Object#is_a? to read more like english? @foo.is_a? Hash, String => Widget