TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Benefits of Writing a DSL in Ruby

80 点作者 edawerd超过 10 年前

11 条评论

vinceguidry超过 10 年前
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 未加载
tel超过 10 年前
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 未加载
habosa超过 10 年前
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.
tcopeland超过 10 年前
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>
qewrffewqwfqew超过 10 年前
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 未加载
rubiquity超过 10 年前
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 未加载
bshimmin超过 10 年前
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 未加载
emson超过 10 年前
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.
thewarrior超过 10 年前
Any ruby experts know why the AttributeScope class is required ? Cant the functionality be included in the containing class ?
评论 #8394009 未加载
CalinBalauru超过 10 年前
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.
VeejayRampay超过 10 年前
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.