Back
  1. Ruby ProTip: Better DSL’s

    January 24, 2011 by nickmulder comments

    DSL’s in Ruby are easy to build. What I am about to show you is far from new, but it’s a great trick and its worth the refresher.

    Consider this DSL

    http://gist.github.com/790023

    Now consider this DSL

    http://gist.github.com/790024

    There is one subtle difference, we don’t need the f’s! Here is how to pull it off:

    http://gist.github.com/790027

    You could also support both if you wanted:

    http://gist.github.com/790029

    When building a DSL it’s better not to be wishy-washy. Choose one version and stick to it.

    I prefer the instance_eval version. (Though I will typically use Proc.new to capture the block. More on that later.)

    @nickmulder


  2. blog comments powered by Disqus