For any struggling to understand Ruby's metaprogramming, the rather short and succinct <i>Metaprogramming Ruby</i> is an excellent read. There's a method to the madness of define_method, method_missing, the various eval methods, the difference between Blocks, Procs, and Lambdas. Most of it revolves around manipulating scopes.<p>It also shows how you can iterate from the ugliest, most powerful, most dangerous <i>eval</i> method by pulling out the functionality you need into less-powerful, safer manipulations. It's all Ruby code until it's actually executed, so it can all be manipulated at runtime.<p><a href="http://www.amazon.com/Metaprogramming-Ruby-Program-Like-Pros/dp/1934356476" rel="nofollow">http://www.amazon.com/Metaprogramming-Ruby-Program-Like-Pros...</a>
While I agree with all of the benefits of DSL writing, I kind of feel like DSLs in Ruby are a bit silly now. There is always a thin line between "lots of function abstraction" and DSL (e.g. a shallow embedding is pretty tough to distinguish from... well, just a library) and that line is even thinner in the fast-and-loose world of Ruby DSLs.<p>It's a vague enough question to lead to nothing but religious wars, but I really want to push people who are interested in (e)DSLs to consider their implementation in typed languages. Haskell is, of course, a favorite of mine, but you can achieve similar things in Scala and the ML family. You can even go <i>much</i> further using something like Idris, mostly by using its stronger dependently typed system.
Wow. That was an amazing article. I have been doing Rails/Ruby for years and never understood any of that. Thanks for putting that together, please consider doing more tutorials for other interesting things going on in ZenPayroll.
That's a great writeup, well done.<p>Along the same lines, here's the ever-entertaining Rich Kilmer talking about DSL experiences on some government projects:<p><a href="http://www.infoq.com/presentations/kilmer-ruby-dsls" rel="nofollow">http://www.infoq.com/presentations/kilmer-ruby-dsls</a>
I don't get it. How is this a DSL?<p>Programmer with a couple of decades' experience here, but no ruby. I have no idea what half the tokens in the example mean, or what changes would be valid beyond editing the four obvious values.
That seems like unnecessary use of metaprogamming and DSLs in my opinion[0]. That problem could be solved with good ol' object-orientation.<p>Also, capturing blocks and instance_eval are notoriously slow.<p>0 - <a href="https://www.youtube.com/watch?v=yuh9COzp5vo" rel="nofollow">https://www.youtube.com/watch?v=yuh9COzp5vo</a><p>1 - <a href="https://speakerdeck.com/sferik/writing-fast-ruby" rel="nofollow">https://speakerdeck.com/sferik/writing-fast-ruby</a>
A good read for anyone just starting out with some of the more intricate parts of Ruby.<p>I often think Ruby is unsurpassed for writing readable, English-like DSLs - perhaps because of the combination of the convenient lassitude of Ruby's syntax (not requiring brackets - in many cases - or semi-colons) and the dangerous power of its metaprogramming abilities. The only other language that perhaps comes close and immediately springs to mind, before I've had my first coffee of the day, is (appropriately) CoffeeScript. Any other takers?
If you would like to see an example for a command line invoice tool I wrote, have a look at this:<p><a href="https://github.com/emson/invoicerb" rel="nofollow">https://github.com/emson/invoicerb</a><p>I wrote it because I used an invoice app, that went bust and I needed something really simple to send out invoices and I was in complete control of.<p>Also I didn't want to use a database so just used Ruby DSL meta files that described my invoice.
Admittedly it probably needs a bit more work... but it was fun to do.
One of the best articlets I've read about the ruby dsl's was on practicingruby. You guys can check it out here <a href="https://practicingruby.com/articles/domain-specific-apis" rel="nofollow">https://practicingruby.com/articles/domain-specific-apis</a> is free now.