# How to write a DSL

**URL:** <https://www.ruby-forum.com/t/how-to-write-a-dsl/169835>\
**Category:** Ruby\
**Created:** [June 9, 2009, 1:12pm UTC](https://www.ruby-forum.com/t/how-to-write-a-dsl/169835 "2009-06-09T13:12:26Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![christoph\_j](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/76d3ee/32.png) [@christoph\_j](https://www.ruby-forum.com/u/christoph_j)\
**Post date:** [June 9, 2009, 1:12pm UTC](https://www.ruby-forum.com/t/how-to-write-a-dsl/169835/1 "2009-06-09T13:12:26Z")

</div>

Hi there,  
I’m new to DSLs and wanted to know if someone could tell me some good  
and  
deep sources (books, long articles, examples, tutorials, etc.) for how  
to  
write DSLs with Ruby. Of course you get some hits with Google, but I  
can’t  
distinct between good and bad ones. If you already delt with it, please  
give  
me a hint.

I’ve seen something which Shoulda is capable of: (that’s a kind of a  
DSL,  
right?)

class RubyMagic \< Test::…  
.  
.  
.  
should “be a minor if younger than 18” do  
.  
this and that  
.  
end  
.  
.  
.  
end

How do you write things like this. I wanted to let the user say  
something  
like this and then proceed his/her request. Is it possible?Is it not  
that  
difficult?

Thanks,

Chris

---

<div class="post-metadata">

**Author:** ![christoph\_j](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/76d3ee/32.png) [@christoph\_j](https://www.ruby-forum.com/u/christoph_j)\
**Post date:** [June 9, 2009, 2:26pm UTC](https://www.ruby-forum.com/t/how-to-write-a-dsl/169835/2 "2009-06-09T14:26:22Z")

</div>

On Tue, Jun 9, 2009 at 1:11 PM, Christoph  
Jasinski[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’m new to DSLs and wanted to know if someone could tell me some good and  
> deep sources (books, long articles, examples, tutorials, etc.)

[http://onestepback.org/articles/lingo/index.html](http://onestepback.org/articles/lingo/index.html)

> **[The State of the DSL Art in Ruby](https://www.infoq.com/presentations/vanderburg-state-of-dsl-ruby/)**
>
> In this talk Glenn Vanderburg discusses what the Ruby community has learned about building DSLs, and shows how to build state-of-the-art DSLs without going overboard.

> **[The 5 Ws of DSLs](https://www.infoq.com/presentations/5W-of-DSL/)**
>
> This presentation recorded at QCon SF 2008 represents an introduction to Domain Specific Languages. Jay Fields responds to the following 5 questions of DSLs: What, Who, Where, When, and Why?

If you are the type of person who learns well by reading source, you  
could have a look at Sinatra:

> **[GitHub - sinatra/sinatra: Classy web-development dressed in a DSL (official /...](https://github.com/sinatra/sinatra/tree/main)**
>
> main

Michael

---

<div class="post-metadata">

**Author:** ![christoph\_j](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/76d3ee/32.png) [@christoph\_j](https://www.ruby-forum.com/u/christoph_j)\
**Post date:** [June 9, 2009, 2:35pm UTC](https://www.ruby-forum.com/t/how-to-write-a-dsl/169835/3 "2009-06-09T14:35:45Z")

</div>

Christoph J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> I’m new to DSLs and wanted to know if someone could tell me some good and  
> deep sources (books, long articles, examples, tutorials, etc.) for how to  
> write DSLs with Ruby. Of course you get some hits with Google, but I can’t  
> distinct between good and bad ones. If you already delt with it, please give  
> me a hint.

While I don’t always agree with the details written in  
[http://martinfowler.com/dslwip/](http://martinfowler.com/dslwip/)  
(notably concerning Lisp), I find that presentation quite exhaustive and  
interesting.

After reading it, you should be able to write any kind of DSL in any  
programming language…

---

<div class="post-metadata">

**Author:** ![christoph\_j](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/76d3ee/32.png) [@christoph\_j](https://www.ruby-forum.com/u/christoph_j)\
**Post date:** [June 9, 2009, 2:41pm UTC](https://www.ruby-forum.com/t/how-to-write-a-dsl/169835/4 "2009-06-09T14:41:37Z")

</div>

Thank’s guy, looks really cool.  
The Pragmatic Programmers have also this here:  
Language Design Patterns: Techniques for Implementing Domain-Specific  
Languages by Terence Parr

This also could be interesting

Anyway, thanks again.  
Cheers,

Chris

---

<div class="post-metadata">

**Author:** ![christoph\_j](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/76d3ee/32.png) [@christoph\_j](https://www.ruby-forum.com/u/christoph_j)\
**Post date:** [June 9, 2009, 2:54pm UTC](https://www.ruby-forum.com/t/how-to-write-a-dsl/169835/5 "2009-06-09T14:54:29Z")

</div>

Am 09.06.2009 14:40 Uhr, schrieb Christoph J.:

> The Pragmatic Programmers have also this here:  
> Language Design Patterns: Techniques for Implementing Domain-Specific  
> Languages by Terence Parr

And in “Design Patterns in Ruby” by Russ Olsen is also an own chapter  
about DSLs; quite good (IMHO).

Stefan

---

<div class="post-metadata">

**Author:** ![christoph\_j](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/76d3ee/32.png) [@christoph\_j](https://www.ruby-forum.com/u/christoph_j)\
**Post date:** [June 9, 2009, 2:28pm UTC](https://www.ruby-forum.com/t/how-to-write-a-dsl/169835/6 "2009-06-09T14:28:02Z")

</div>

Christoph J. wrote:

> I’ve seen something which Shoulda is capable of: (that’s a kind of a  
> DSL,  
> right?)
> 
> class RubyMagic \< Test::…  
> .  
> .  
> .  
> should “be a minor if younger than 18” do  
> .  
> this and that  
> .  
> end  
> .  
> .  
> .  
> end
> 
> How do you write things like this. I wanted to let the user say  
> something  
> like this and then proceed his/her request. Is it possible?Is it not  
> that  
> difficult?

I’d suggest that a good starting point is to install shoulda and look at  
its source code.

Note that

should “be a minor” do  
stuff  
end

is just a regular method call with a block. Try this:

def should(\*args)  
puts “Got args: #{args.inspect}”  
if block\_given?  
puts “Got a block”  
yield  
end  
end

should “be a minor” do  
puts “I am here”  
end
