Ruby DSL 与普通 API 的区别
Ruby DSL 有哪些定义特征可以将其与常规 API 区分开来?
What are some defining characteristics of a Ruby DSL that separate it from just a regular API?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
当您使用 API 时,您会以命令式方式实例化对象并调用方法。另一方面,好的 DSL 应该是声明性的,代表问题域中的规则和关系,而不是要执行的指令。此外,理想情况下,DSL 应该可由非程序员读取和修改(API 则不然)。
另请记住内部 DSL 和外部 DSL 之间的区别。
When you use an API you instantiate objects and call methods in an imperative manner. On the other hand a good DSL should be declarative, representing rules and relationships in your problem domain, not instructions to be executed. Moreover ideally DSL should be readable and modifiable by somebody who is not a programmer (which is not the case with APIs).
Also please keep in mind the distinction between internal and external DSLs.
DSL(领域特定语言)是一个被过度炒作的术语。如果您只是使用一种语言的子集(例如 Ruby),那么它与原始语言有何不同?答案是,不是。
但是,如果您对源文本进行一些预处理以引入核心语言中未找到的新语法或新语义,那么您确实拥有一种新语言,这可能是特定于领域的。
DSL (domain specific language) is an over-hyped term. If you are simply using a sub-set of a language (say Ruby), how is it a different language than the original? The answer is, it isn't.
However, if you do some preprocessing of the source text to introduce new syntax or new semantics not found in the core language then you indeed have a new language, which may be domain-specific.
Ruby 的诗歌模式和运算符重载的结合确实提供了同时具有合法 Ruby 语法和合理 DSL 的可能性。
XML 的持续恶化确实表明,也许内置在所有这些配置文件中的简单 DSL 并没有完全被误导。
The combination of Ruby's poetry mode and operator overloading does present the possibility of having something that is at the same time legal Ruby syntax and a reasonable DSL.
And the continued aggravation that is XML does show that perhaps the simple DSL built into all those config files wasn't completely misguided..
创建 DSL:
向 Object 类添加新方法,以便您可以像调用内置语言结构一样调用它们。 (请参阅 rake)
在自定义对象或对象集上创建方法,然后让脚本文件在顶级对象的上下文中运行语句。 (请参阅 capistrano)
API 设计:
在自定义对象或一组对象上创建方法,以便用户创建一个对象来使用这些方法。
将方法创建为类方法,以便用户在所有方法前面添加类名前缀。
将方法创建为
将方法创建为用户包含或扩展以在其自定义对象中使用方法的混合。
所以,是的,他们之间的界限很窄。通过添加一个在正确上下文中运行脚本文件的方法,将一组自定义对象转换为 DSL 非常简单。
Creating a DSL:
Adding new methods to the Object class so that you can just call them as if they were built-in language constructs. (see rake)
Creating methods on a custom object or set of objects, and then having script files run the statements in the context of a top-level object. (see capistrano)
API design:
Creating methods on a custom object or set of objects, so the user creates an object to use the methods.
Creating methods as class methods, so that the user prefixes the classname in front of all the methods.
Creating methods as a mixin that users include or extend to use the methods in their custom objects.
So yes, the line is thin between them. It's trivial to turn a custom set of objects into a DSL by adding one method that runs a script file in the right context.
对我来说,DSL 和 API 之间的区别在于,如果 DSL 不是由该领域的人编写为 Ruby 的子语言,那么它至少可以被理解(并验证)。
例如,您可以让财务分析师在 Ruby DSL 中为股票交易应用程序编写规则,而他们永远不必知道自己正在使用 Ruby。
The difference between a DSL and an API to me is that a DSL could be at least understood (and verified) if not written as a sub-language of Ruby by someone in that domain.
For example, you could have financial analysts writing rules for a stock trading application in a Ruby DSL and they would never have to know they were using Ruby.
事实上,它们是同一件事。 DSL 通常是通过 Ruby 中的正常语言机制实现的,因此从技术上讲它们都是 API。
然而,为了让人们认识到某个东西是 DSL,它通常最终会向现有类添加看起来像声明性语句的内容。类似于 ActiveRecord 中的验证器和关系声明。
看起来像 DSL,而下面的则不是:
它们都将由普通的 Ruby 代码实现。只是其中一个看起来像是有很酷的新语言结构,而另一个看起来相当平淡(并且过于冗长,等等)
They are, in fact, the same thing. DSLs are generally implemented via the normal language mechanisms in Ruby, so technically they're all APIs.
However, for people to recognize something as a DSL, it usually ends up adding what look like declarative statements to existing classes. Something like the validators and relationship declarations in ActiveRecord.
looks like a DSL, while the following doesn't:
They're both going to be implemented by normal Ruby code. It's just that one looks like you've got cool new language constructs, while the other seems rather pedestrian (and overly verbose, etc. etc.)