gpt4 book ai didi

ruby - Ruby 中的消息是实现细节还是基础

转载 作者:数据小太阳 更新时间:2023-10-29 07:44:01 26 4
gpt4 key购买 nike

我的理解是,对 Ruby 对象的方法调用正在向该对象发送消息(这可以使用 send 方法手动完成)。

当我使用 Python 时,我不会考虑如何处理方法/函数调用;如果它已定义或可用,则它会执行,否则我会收到错误消息。

1) Ruby 对消息的使用是否独特(即 Python 是否也在幕后使用消息)?

2) Ruby 对消息的使用只是一个实现细节,还是它是您应该如何使用 Ruby 的基础(即,如果我忽略它并继续像在 Python 中那样调用我的方法,我是否会错过某些东西) ?

最佳答案

消息是基本的,不仅在 Ruby 中,而且在一般的 OO 中也是如此。

术语“面向对象”是由 Alan Kay 发明的,他定义了它 this way :

OOP to me means only messaging, local retention and protection and hiding of state-process, and extreme late-binding of all things.

用更现代的话来说:

  • 消息传递
  • 封装
  • 后期绑定(bind)

在实现方面,消息传递是一个后期绑定(bind)的过程调用,如果过程调用是后期绑定(bind)的,那么您在设计时就无法知道您要调用什么,因此您无法关于状态的具体表示的任何假设。所以,它实际上是关于消息传递的,后期绑定(bind)是消息传递的一种实现,封装是它的结果。

他后来澄清了“The big idea is 'messaging'”,并后悔将其称为“面向对象”而不是“面向消息”,因为“面向对象”一词将重点放在了不重要的事情(对象)上并分散真正重要的事情(消息):

Just a gentle reminder that I took some pains at the last OOPSLA to try to remind everyone that Smalltalk is not only NOT its syntax or the class library, it is not even about classes. I'm sorry that I long ago coined the term "objects" for this topic because it gets many people to focus on the lesser idea.

The big idea is "messaging" -- that is what the kernal of Smalltalk/Squeak is all about (and it's something that was never quite completed in our Xerox PARC phase). The Japanese have a small word -- ma -- for "that which is in between" -- perhaps the nearest English equivalent is "interstitial". The key in making great and growable systems is much more to design how its modules communicate rather than what their internal properties and behaviors should be. Think of the internet -- to live, it (a) has to allow many different kinds of ideas and realizations that are beyond any single standard and (b) to allow varying degrees of safe interoperability between these ideas.

(当然,在今天,大多数人甚至不关注对象而是关注类,这是错误的。)

消息传递是 OO 的基础,无论是作为隐喻还是作为一种机制。

如果您向某人发送一条消息,您不知道他们用这条消息做了什么。您可以观察到的唯一是他们的 react 。你不知道他们是否自己处理消息(即对象是否有方法),他们是否将消息转发给其他人(委托(delegate)/代理),他们是否理解它。这就是封装的意义所在,这就是 OO 的意义所在。您甚至无法将代理与真实事物区分开来,只要它按照您的期望做出响应即可。

“消息传递”的更“现代”术语是“动态方法调度”或“虚拟方法调用”,但这失去了隐喻,而侧重于机制。

On Understanding Data Abstraction, Revisited中也有类似的观点通过 William R. Cook还有他的Proposal for Simplified, Modern Definitions of "Object" and "Object Oriented" .

Dynamic dispatch of operations is the essential characteristic of objects. It means that the operation to be invoked is a dynamic property of the object itself. Operations cannot be identified statically, and there is no way in general to exactly what operation will executed in response to a given request, except by running it. This is exactly the same as with first-class functions, which are always dynamically dispatched.

在 Smalltalk-72 中,甚至没有任何对象! 只有 消息流被解析、重写和重新路由。首先是方法(解析和重新路由消息流的标准方法),然后是对象(共享一些私有(private)状态的方法分组)。继承来得晚得多,引入类只是作为支持继承的一种方式。如果 Kay 的研究小组已经了解原型(prototype),他们可能一开始就不会引入类。

关于ruby - Ruby 中的消息是实现细节还是基础,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25179914/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com