Why Ruby is a good language for DSLs

I’ve heard people say that Ruby is an excellent language to use for creating DSLs (domain specific languages, little custom languages written for a specific problem domain) but I never understood why. I don’t know Ruby, so it was hard for me to imagine. But I heard John Lam give a very clear explanation on the ALT.NET podcast. He said that since Ruby doesn’t require parentheses around function call arguments, you can make function names look like keywords. For example, you could write functions “skin” and “this” and make them look like keywords. The expression “skin this cat” is actually parsed as skin(this, cat) but the former can look more natural.

Another interesting quote from John Lam in that interview was “only a masochist would program office from C#.” He said that because of late binding, automating Microsoft Office is much easier from Ruby. The office object model was designed to be used from a language with late bindingĀ  (i.e. VB) and so Ruby is easier to use than C#.

4 thoughts on “Why Ruby is a good language for DSLs

  1. I’ve been using ruby for a while now and I am still often pleasantly surprised when I see a clever DSL built in ruby. The web framework Sinatra and the graphics language Shoooes are good, recent examples.

    Just a nit-pick, the example you use `skin this cat` would actually be interpreted as `skin(this,cat)`.

  2. In Forth, every function name becomes a keyword, and there are no parentheses. Those features don’t necessarily make Forth suitable for all types of DSLs, but it easily meets the criteria you’ve laid out. And if you can assimilate the idiosyncratic RPN syntax, you might find it to be a surprisingly powerful little language.

    Although I haven’t used Forth in about 20 years, of all the languages I’ve learned, Forth is the one that has had the greatest impact on inducing the adoption of good practices regarding code factoring and documentation.

  3. I haven’t used Forth but I have played around with an interesting language called Factor which seems a natural successor to Forth in that it is stack-based has a similar syntax.

Comments are closed.