Designing Ruby DSLs
• Don’t try to do an abstract metamodel ﬁrst
• Capture your DSL concepts in valid Ruby
syntax, but don’t worry about implementation
• Iterate over your Ruby DSL syntax until authors
agree that it faithfully represents the domain,
then work on the implementation
Let the DSL you devise
guide your implementation
Kind of like TDD, don’t do more
than what you need to make your
DSL execute correctly.
DSLs that reﬂect business
documents such as
contracts are great
Designing a DSL that’s as close as possible to the document it
reﬂects makes veriﬁcation of the system much easier!
Agile DSL Development?
• Start with short iterations over the design
• Incorporate end-user feedback, pair with them if
• Do TDD your context code
• Do refactor your context code often, but avoid
“The fascinating thing is that, in my
experience, most well-written Ruby
programs are already a DSL, just by
nature of Ruby’s syntax.”
Jamis Buck, 37signals
Your DSL is simply methods on an object
DSL as methods on some ancestor class, and subclasses
can then use those methods to tweak the behavior of
themselves and their subclasses
Your application deﬁnes the DSL as top-level methods, and
then invokes load with the path to your DSL script. When
those methods are called in the conﬁguration ﬁle, they
modify some central (typically global) data, which your
application uses to determine how it should execute.
Your DSL is deﬁned as methods of some object, but that
object is really just a “sandbox”. Interacting with the
object’s methods modify some state in the sandbox, which
is then queried by the application.
Sandboxing is useful for
maintained scripts kept in
Just load up the script and execute it in the sandbox at
runtime.Vary behavior by changing the execution context.
You’ll end up coding a
metamodel when writing
Your metamodel will be better than if you had tried to
write it ﬁrst based on pure analysis!
Ruby Features used by
• Symbols, less noisy than strings
• Blocks, enabling delayed evaluation of code
• Modules, for cleaner separation of code
• Splats, for handling parameter arrays
• eval, instance_eval, and class_eval
• deﬁne_method and alias_method
It’s a different way of thinking about
writing code, and as such needs to be
learned by doing, not by reading.
Experimentation is the key!
Jamis Buck, 37signals
DSLs that are maintained by business users
editable BNL scripts into
How crazy do you want to get?
Programmers can deal
with more complexity
than domain experts
Internal DSLs might not be the best choice
Hybrid Ruby BNLs
• Our experiment with an external DSLs
• Business rules are stored in the database as text
• Pre-parser appends an underscore to each word,
replaces white space with '.' and changes each
number to be an argument to a method that
stores the numeric values
• Once the preparse has executed, each line is
instance_eval’d to create rule objects
• Rule objects are then evaluated using the
learned doing BNLs
• Keep the language consistent
• Creating a language that can execute a line
at a time greatly simpliﬁes syntax checking
• Keep the language simple. Subject matter
experts get fussy about complexity.
• BNL code is DAMP, not DRY
Descriptive and Maintainable Phrases
More on Ruby DSLs