
Ruby DSL Handbook - saturnflyer
http://clean-ruby.com/dsl
======
timruffles
I think it needs a foreword: "your problem likely is nowhere _near_ needing a
DSL, please don't write one just because it makes you look clever".

Signed: everyone who's ever used a DSL which could have been written better as
a couple of dozen functions.

~~~
tel
Almost any problem can be meaningfully and usefully solved by introduction of
a DSL. It's a remarkably flexible technique.

Ruby is a really bad language to write good DSLs in. Metaprogramming is an
awkward replacement for better DSL facilities.

Maybe this book can demonstrate better techniques?

~~~
ritchiea
Interesting, that runs counter to the prevailing sentiment that Ruby's
metaprogramming makes it great for writing DSLs. What language features do you
prefer for writing DSLs?

~~~
tel
Typing, phantom typing, straightforward lambdas, sum types, tail-recursion,
direct syntax abstractions (both Lisp and Idris are great examples).

I find that metaprogramming in Ruby is a tremendous hack for getting something
like syntax abstraction and something like control over evaluation order.
Personally, syntax abstraction doesn't feel super important for deep or
shallow embedded DSLs (though it's obvious a big deal in external DSLs if you
want to walk that minefield). You need more than nothing, but making it "human
language like" is ridiculous.

Controlling flow is a big part of it though. Ruby's block syntax does a lot
here, but frankly functions/abstractions never feel first class in Ruby---only
Objects do and Objects are too heavyweight.

------
thirdtruck
Really looking forward to reading this. I got hooked on the Ruby DSL bug after
reading about it in _Metaprogramming Ruby_
([https://pragprog.com/book/ppmetr2/metaprogramming-
ruby-2](https://pragprog.com/book/ppmetr2/metaprogramming-ruby-2)), and I've
carried the concepts with me wherever I could.

------
tcopeland
On a related note, here's Rich Kilmer talking about Ruby DSLs way back in
2008:

[http://www.infoq.com/presentations/kilmer-ruby-
dsls](http://www.infoq.com/presentations/kilmer-ruby-dsls)

Good stuff from a guy who's done some pretty massive DSLs on DARPA projects.

------
dkarapetyan
Is there some kind of sample chapter?

~~~
saturnflyer
Not yet. But you can signup for a 5-email crash course. It's being updated
almost every day now as I add more content.

------
davexunit
I've never been a fan of using "DSL" in the context of Ruby. Sure, you can do
some metaprogramming, but you cannot create new syntax, change order of
evaluation, etc., which I feel is a fundamental requirement for creating real
EDSLs. One simple consequence of this is that you end up having to quote a lot
of things.

~~~
saturnflyer
This surprises me. Can you offer an example of those types of problems in some
Ruby code?

~~~
davexunit
Let's use 'attr_reader' as a simple example. It defines a method that returns
the value of a member variable. It would be nice to refer to the variable name
literally, but this is not possible:

    
    
        attr_reader foo
    

The 'foo' above would evaluate to the contents of the variable 'foo'. So, you
have to do this:

    
    
        attr_reader :foo
    

Because we cannot control how this code evaluated, we must quote 'foo'. This
greatly limits what we can express.

~~~
towelrod
That's one of the things I like about Ruby DSLs. Because the DSL is just Ruby,
you can do things like:

    
    
        [:foo, :bar].each do |attribute|
            attr_reader attribute
        end
    

You probably wouldn't want do actually do that with attr_reader, but it is
just an example.

~~~
davexunit
Typically, you'd write syntactic sugar on top of regular functions, so that
the underlying API is available in the host language to do things like in your
example. Using Ruby's class syntax as an example, it gives you a convenient
means to describe classes, but underneath that is an API for programmatic
manipulation of class objects.

