View on GitHub


Docile keeps your Ruby DSLs tame and well-behaved

Download this project as a .zip file Download this project as a tar.gz file


Docile is a small, self-contained Ruby library, that lets you map a DSL (domain specific language) to your Ruby objects in a snap.

Let's take a look

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 using Docile like this:

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

Big deal, right? Well guess what! It handles the rough edges for you, so you can use functions, instance variables, and local variables in your DSL with no sweat:

def question
  "What is the answer...?"

@answer = 42

knowledge = { "towel" => true }

Docile.dsl_eval({}) do
  store question, @answer
  merge! knowledge
# => {"What is the answer...?"=>42, "towel"=>true}


Ok, I want to learn more!