acts_as_conference 2008 ~ Advanced DSLs with Ruby ~ Neal Ford

Advanced DSLs with Ruby ~ Neal Ford

Do not parse text using regular expressions

def process_definition(definition)
  instance_eval polish_text(definition)
end

‘ingredient “flour” has Protein => 11.5, Lipid => 1.45’

ingredient is a method, flour is first parameter, has is a “bubble word”, Hash is second parameters

class NutritionProfileDefinition
  def self.const_missing(sym)
    sym.to_s.downcase
  end
end

def consists_of(&block)
  instance_eval &block
end

Recipe.new(:name => ‘gravy’).consists_of do
  …
end

DSLs specify intent without specifying implementation.

SQL is a DSL that gets parsed; it’s not built on something else.

There are tools coming out soon that will make creating external DSLs better. Metagram coming out.

Antler is another “language workbench” in other languages that let you create your own DSLs.

The most successful internal DSLs at ThoughtWorks are those where business analysts need to write new rules really fast. Business analysts didn’t know Ruby. ThoughtWorks created a sandbox where the business analysts can experiment with their rules before pushing into production. The sandbox came in the form of an editor.

Q: “Are there security concerns with instance_eval?”
A: “It’s like performance. Build your security around your particular needs, when you need it.”

He’s 100% in favor RIGHT NOW of internal DSLs over external DSLs. Once the Ruby “language workbenches” come out, external DSLs may be preferable.

Rails is an internal DSL.