-
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
Now consider this DSL
There is one subtle difference, we don’t need the f’s! Here is how to pull it off:
You could also support both if you wanted:
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.)
blog comments powered by Disqus
