This post originated from an RSS feed registered with Ruby Buzz
by Obie Fernandez.
Original Post: The Magic of Ruby DSL
Feed Title: Obie On Rails (Has It Been 9 Years Already?)
Feed URL: http://jroller.com/obie/feed/entries/rss
Feed Description: Obie Fernandez talks about life as a technologist, mostly as ramblings about software development and consulting. Nowadays it's pretty much all about Ruby and Ruby on Rails.
One of my favorite aspects of Ruby is the elegance with which you can create internal domain-specific languages, as described by Martin Fowler in his paper about language workbenches. To their detriment, many of the developers I try to teach internal DSL concepts get stuck on the elegance aspect -- of the Ruby language syntax! See Joe's description as an example. To my great relief and satisfaction, today the light bulbs finally appeared over my teammates heads!
Sure, the syntax of Ruby is such that you can write nice-looking DSL code, but the most appealing aspect of internal DSL's in Ruby has nothing to do with syntax. Nothing at all. The primary benefit is the elegance with which you can achieve flexible reuse of your DSL scripts while maintaining semantic consistency. It is extremely easy to swap "parsers", if you will, for your DSL script, by executing them with varied bindings. In fact, the context (motivation) of reuse can be wildly different.
For example, here at the Juniper project (our team blog is coming soon, I promise!) we are authoring a DSL that, depending on the context in which it is eval'd, generates 1) INSERT-SELECT sql statements 2) stored procedures 3) XHTML code to be displayed and manipulated in a web browser.
Effectively leveraging the power of DSL-based development together with Ruby is a significant theme in my upcoming book for Addison Wesley: Extending Rails Into the Enterprise.