为什么Ruby人说他们不需要接口? [英] Why do Ruby people say they don't need interfaces?

查看:170
本文介绍了为什么Ruby人说他们不需要接口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ruby​​是否与其他OOP语言(例如:PHP)有所不同,这使得接口无用?它是否有某种替代品?

Does ruby have something different to other OOP languages (eg: PHP) that makes interfaces useless? Does it have some kind of replacement for this?

编辑:

一些澄清:


  • 在其他语言(例如:PHP)中,您不需要接口(在代码级别它们不是必需的)。您可以使用它们签订合同,以改进软件的架构。因此,肯定'在红宝石中你不需要接口/用其他语言你需要接口因为XXX'是假的。

  • In other languages (eg: PHP), you don't "need" interfaces (they are not mandatory at code level). You use them to make a contract, to improve the architecture of the software. Therefore, the affirmation 'in ruby you don't need interfaces / in other languages you need interfaces because XXX' is false.

不,mixins不是接口,它们完全不同(PHP 5.4实现了mixins)。你有没有使用过接口?

No, mixins are not interfaces, they are a complete different thing (PHP 5.4 implements mixins). Have you even used interfaces?

是的,PHP是OOP。语言不断发展,欢迎来到现在。

Yes, PHP is OOP. Languages evolve, welcome to the present.

推荐答案

嗯,这是一个共识当一个对象在Ruby中传递时,它不是经过类型检查的。 Java和PHP中的接口是一种确认对象符合某个契约或类型的方法(因此某些东西可能 Serializable Authorizable 顺序以及你想要的任何其他内容。

Well, it's a consensus that when an object is passed in Ruby it's not type-checked. Interfaces in Java and PHP are a way to affirm that an object complies to a certain contract or "type" (so something might be Serializable, Authorizable, Sequential and whatever else that you want).

然而,在Ruby中有由于接口一致性未在方法签名中检查,因此没有正式的合同概念,其中接口将履行某些有意义的角色。例如,参见 Enumerable 。当您将其混合到对象中时,您使用的是功能,而不是声明您的对象是 Enumerable 。让你的对象 Enumerable 的唯一好处就是定义每个(& blk)你会自动获得地图选择和朋友免费。你可以完美地拥有一个实现 Enumerable 提供的所有方法的对象,但不会在模块中混合,它仍然可以工作。

However, in Ruby there is no formalized notion of a contract for which interfaces would fulfill some meaningful role as interface conformance is not checked in method signatures. See, for example, Enumerable. When you mix it into your object you are using its functionality as opposed to declaring that your object is Enumerable. The only benefit of having your object being Enumerable is that having defined each(&blk) you automatically get map, select and friends for free. You can perfectly have an object which implements all of the methods provided by Enumerable but does not mix in the module and it would still work.

例如,对于Ruby中需要IO对象的任何方法,你可以输入一些 nothing 与IO有关的东西,然后它会因错误而爆炸或 - 如果你正确地实现了你的IO存根 - 即使你的传递对象没有被声明为IO-ish,它也会正常工作。

For example, for any method in Ruby that expects an IO object you could feed in something that has nothing to do with an IO, and then it would explode with an error or - if you implemented your IO stub correctly - it will work just fine even though your passed object is not declared to be "IO-ish".

背后的想法来自于事实上,Ruby中的对象并没有真正得到美化的哈希表,并且标签被打到它们上(然后有一些额外的标签告诉解释器或编译器这个对象有接口X因此它可以在上下文Y中使用)但是一个封闭的实体响应消息。因此,如果一个对象响应一个特定的消息,那么它将填写合同,如果它没有响应该消息 - 那么就会出现错误。

The idea behind that comes from the fact that objects in Ruby are not really glorified hash tables with a tag slapped onto them (which then have some extra tags that tell the interpreter or the compiler that this object has interface X therefore it can be used in context Y) but an enclosed entity responding to messages. So if an object responds to a specific message it fullfils the contract, and if it does not respond to that message - well then an error is raised.

所以没有接口可以通过模块的存在进行部分补偿(模块可以包含您在不对呼叫者/消费者做任何类型承诺的情况下达到的功能),部分由消息传递的传统而不是类型化的dicts进行补偿。

So the absence of interfaces is compensated partially by the presence of Modules (which can contain functionality that you reach for without doing any type promises to the caller/consumer) and partially by the tradition of message-passing as opposed to typed dicts.

你应该观看Jim Weirich的一些演讲,因为他广泛接触过这个主题。

You should watch some presentations by Jim Weirich since he touches on the subject extensively.

这篇关于为什么Ruby人说他们不需要接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆