Skip to content
Docile keeps your Ruby DSLs tame and well-behaved
Latest commit 41546c8 May 21, 2016 @ms-ati Bump ruby-version to 2.2.3


Gem Version Gem Downloads Yard Docs Join the chat at

Build Status Dependency Status Code Climate Code Coverage Inline docs

Ruby makes it possible to create very expressive Domain Specific Languages, or DSL's for short. However, it requires some deep knowledge and somewhat hairy meta-programming to get the interface just right.

"Docile" means Ready to accept control or instruction; submissive [1]

Instead of each Ruby project reinventing this wheel, let's make our Ruby DSL coding a bit more docile...


Basic: Ruby Array as DSL

Let's say that we want to make a DSL for modifying Array objects. Wouldn't it be great if we could just treat the methods of Array as a DSL?

with_array([]) do
  push 1
  push 2
  push 3
#=> [1, 3]

No problem, just define the method with_array like this:

def with_array(arr=[], &block)
  Docile.dsl_eval(arr, &block)


Build a Pizza

Mutating (changing) an Array instance is fine, but what usually makes a good DSL is a Builder Pattern.

For example, let's say you want a DSL to specify how you want to build a Pizza:

@sauce_level = :extra

pizza do
  sauce @sauce_level
#=> #<Pizza:0x00001009dc398 @cheese=true, @pepperoni=true, @bacon=false, @sauce=:extra>

And let's say we have a PizzaBuilder, which builds a Pizza like this:

Pizza =, :pepperoni, :bacon, :sauce)

class PizzaBuilder
  def cheese(v=true); @cheese = v; self; end
  def pepperoni(v=true); @pepperoni = v; self; end
  def bacon(v=true); @bacon = v; self; end
  def sauce(v=nil); @sauce = v; self; end
  def build!!@cheese, !!@pepperoni, !!@bacon, @sauce)
#=> #<Pizza:0x00001009dc398 @cheese=true, @pepperoni=true, @bacon=false, @sauce=:extra>

Then implement your DSL like this:

def pizza(&block)
  Docile.dsl_eval(, &block).build

It's just that easy!

Multi-level and Recursive DSLs

Docile is a very easy way to write a multi-level DSL in Ruby, even for a recursive data structure such as a tree:

Person =, :mother, :father)

person {
  name 'John Smith'
  mother {
    name 'Mary Smith'
  father {
    name 'Tom Smith'
    mother {
      name 'Jane Smith'

#=> #<struct Person name="John Smith",
#                   mother=#<struct Person name="Mary Smith", mother=nil, father=nil>,
#                   father=#<struct Person name="Tom Smith",
#                                          mother=#<struct Person name="Jane Smith", mother=nil, father=nil>,
#                                          father=nil>>

See the full person tree example for details.

Block parameters

Parameters can be passed to the DSL block.

Supposing you want to make some sort of cheap Sinatra knockoff:

@last_request = nil
respond '/path' do |request|
  puts "Request received: #{request}"
  @last_request = request

def ride bike
  # Play with your new bike

respond '/new_bike' do |bike|

You'd put together a dispatcher something like this:

require 'singleton'

class DispatchScope
  def a_method_you_can_call_from_inside_the_block

class MessageDispatch
  include Singleton

  def initialize
    @responders = {}

  def add_responder path, &block
    @responders[path] = block

  def dispatch path, request
    Docile.dsl_eval(, request, &@responders[path])

def respond path, &handler
  MessageDispatch.instance.add_responder path, handler

def send_request path, request
  MessageDispatch.instance.dispatch path, request

Functional-Style Immutable DSL Objects

Sometimes, you want to use an object as a DSL, but it doesn't quite fit the imperative pattern shown above.

Instead of methods like Array#push, which modifies the object at hand, it has methods like String#reverse, which returns a new object without touching the original. Perhaps it's even frozen in order to enforce immutability.

Wouldn't it be great if we could just treat these methods as a DSL as well?

s = "I'm immutable!".freeze

with_immutable_string(s) do

#=> "I'm immutable!"

No problem, just define the method with_immutable_string like this:

def with_immutable_string(str="", &block)
  Docile.dsl_eval_immutable(str, &block)

All set!


  1. Method lookup falls back from the DSL object to the block's context
  2. Local variable lookup falls back from the DSL object to the block's context
  3. Instance variables are from the block's context only
  4. Nested DSL evaluation, correctly chaining method and variable handling from the inner to the outer DSL scopes
  5. Alternatives for both imperative and functional styles of DSL objects


$ gem install docile



Works on all ruby versions since 1.8.7, or so Travis CI tells us.

Used by some pretty cool gems to implement their DSLs, notably including SimpleCov. Keep an eye out for new gems using Docile at the Ruby Toolbox.

Note on Patches/Pull Requests

  • Fork the project.
  • Setup your development environment with: gem install bundler; bundle install
  • Make your feature addition or bug fix.
  • Add tests for it. This is important so I don't break it in a future version unintentionally.
  • Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
  • Send me a pull request. Bonus points for topic branches.

Copyright & License

Copyright (c) 2012-2016 Marc Siegel.

Licensed under the MIT License, see LICENSE for details.

Something went wrong with that request. Please try again.