TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Benefits of Writing a DSL in Ruby

80 pointsby edawerdover 10 years ago

11 comments

vinceguidryover 10 years ago
For any struggling to understand Ruby&#x27;s metaprogramming, the rather short and succinct <i>Metaprogramming Ruby</i> is an excellent read. There&#x27;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&#x27;s all Ruby code until it&#x27;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:&#x2F;&#x2F;www.amazon.com&#x2F;Metaprogramming-Ruby-Program-Like-Pros...</a>
评论 #8392528 未加载
评论 #8394309 未加载
telover 10 years ago
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 &quot;lots of function abstraction&quot; 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&#x27;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.
评论 #8394753 未加载
habosaover 10 years ago
Wow. That was an amazing article. I have been doing Rails&#x2F;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.
tcopelandover 10 years ago
That&#x27;s a great writeup, well done.<p>Along the same lines, here&#x27;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:&#x2F;&#x2F;www.infoq.com&#x2F;presentations&#x2F;kilmer-ruby-dsls</a>
qewrffewqwfqewover 10 years ago
I don&#x27;t get it. How is this a DSL?<p>Programmer with a couple of decades&#x27; 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.
评论 #8397040 未加载
rubiquityover 10 years ago
That seems like unnecessary use of metaprogamming and DSLs in my opinion[0]. That problem could be solved with good ol&#x27; 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:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=yuh9COzp5vo</a><p>1 - <a href="https://speakerdeck.com/sferik/writing-fast-ruby" rel="nofollow">https:&#x2F;&#x2F;speakerdeck.com&#x2F;sferik&#x2F;writing-fast-ruby</a>
评论 #8394150 未加载
bshimminover 10 years ago
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&#x27;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&#x27;ve had my first coffee of the day, is (appropriately) CoffeeScript. Any other takers?
评论 #8393025 未加载
评论 #8394181 未加载
评论 #8393107 未加载
评论 #8393009 未加载
评论 #8392994 未加载
评论 #8393791 未加载
emsonover 10 years ago
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:&#x2F;&#x2F;github.com&#x2F;emson&#x2F;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&#x27;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.
thewarriorover 10 years ago
Any ruby experts know why the AttributeScope class is required ? Cant the functionality be included in the containing class ?
评论 #8394009 未加载
CalinBalauruover 10 years ago
One of the best articlets I&#x27;ve read about the ruby dsl&#x27;s was on practicingruby. You guys can check it out here <a href="https://practicingruby.com/articles/domain-specific-apis" rel="nofollow">https:&#x2F;&#x2F;practicingruby.com&#x2F;articles&#x2F;domain-specific-apis</a> is free now.
VeejayRampayover 10 years ago
Ruby is really great for metaprogramming. To the point where you have to refrain yourself from sprinkling too much of it all over the place at times.