Ruby 是否有元对象协议?如果没有,是否可以实现一个?

发布于 2024-09-29 18:36:40 字数 87 浏览 5 评论 0原文

请原谅我的无知,但是什么是元对象协议?Ruby 有吗?如果没有,是否可以为 Ruby 实现一个?如果 Ruby 拥有元对象协议,那么元对象协议可能具有哪些功能?

Pardon my ignorance, but What is a Metaobject protocol, and does Ruby have one? If not, is it possible to implement one for Ruby? What features might a Metaobject protocol possess if Ruby was to have one?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一场信仰旅途 2024-10-06 18:36:40

什么是元对象协议?

我遇到的最好的描述来自Class::MOP 文档:

元对象协议是对象系统的 API。

更具体地说,它抽象了对象系统的组件(类、对象、方法、对象属性等)。然后可以使用这些抽象来检查和操作它们所描述的对象系统。

可以说,任何对象系统都有两个 MOP;隐式 MOP 和显式 MOP。隐式 MOP 处理方法分派或继承等事情,这些事情作为对象系统工作方式的一部分自动发生。显式 MOP 通常处理对象系统的内省/反射功能。

所有对象系统都有隐式 MOP。没有一个,他们就无法工作。显式 MOP 不太常见,并且根据语言的不同,可以从限制性(Java 或 C# 中的反射)到广泛开放(CLOS 是一个完美的例子)。

Ruby 有吗?

根据 Reopeningbuiltinclasses, redefiningbuiltin 上的此线程函数? Perlmonks 文章我认为答案是(至少从最严格意义上讲,什么是 MOP)。

显然这里有一些回旋余地,因此可能值得在 SO 的 Perl 方面发布一个问题,因为 Class::MOP / 驼鹿 作者确实回答了那里的问题

What is a Metaobject protocol?

The best description I've come across is from the Class::MOP documentation:

A meta object protocol is an API to an object system.

To be more specific, it abstracts the components of an object system (classes, object, methods, object attributes, etc.). These abstractions can then be used to inspect and manipulate the object system which they describe.

It can be said that there are two MOPs for any object system; the implicit MOP and the explicit MOP. The implicit MOP handles things like method dispatch or inheritance, which happen automatically as part of how the object system works. The explicit MOP typically handles the introspection/reflection features of the object system.

All object systems have implicit MOPs. Without one, they would not work. Explicit MOPs are much less common, and depending on the language can vary from restrictive (Reflection in Java or C#) to wide open (CLOS is a perfect example).

Does Ruby have one?

According to this thread on Reopening builtin classes, redefining builtin functions? Perlmonks article I think the answer is no (at least in the strictest sense of what a MOP is).

Clearly there is some wriggle room here so it might be worth posting a question in the Perl side of SO because the Class::MOP / Moose author does answer questions there.

我喜欢麦丽素 2024-10-06 18:36:40

如果您仔细查看定义,您会发现 Ruby 确实有 MOP。是不是像CLOS里的那样?不,CLOS 是一个元循环 MOP,它很棒(我什至可以说是天才),但它不是唯一真正的方法,请看一下 Smalltalk。要实现(假设是基本的)MOP,您需要的只是提供允许运行时执行以下操作的函数:

  1. 创建或删除新类
  2. 创建新属性或方法
  3. 使类从不同的类继承(“更改类结构” )
  4. 生成或更改定义类方法的代码。

Ruby 提供了一种方法来完成这一切。

旁注:Class::MOP 的作者是对的(恕我直言),它声称使用元循环 MOP 可以做的一些事情在 Ruby 中很难做到(免责声明:我有零, zilch, nada Perl 知识,所以我在想 Smalltalk(如 MOP)与 CLOS(如 MOP),但大多数都是非常具体的(我在这里考虑元类实例化),并且有一些方法可以让事情在没有他们。我认为这完全取决于你的观点,元循环 MOP 更酷,但更多的是在学术方面,而非元循环 MOP 更实用且更容易实现。

If you look closer to the definition, youll see that Ruby does have a MOP. Is it like the one in CLOS? No, CLOS is a meta-circular MOP which is great (I'd even say genius), but it's not the one true way, take a look at Smalltalk. To implement a (let's say basic) MOP all you need is to provide functions that allow your runtime to:

  1. Create or delete a new class
  2. Create a new property or method
  3. Cause a class to inherit from a different class ("change the class structure")
  4. Generate or change the code defining the methods of a class.

And Ruby provides a way to do all that.

On a side note: The author of Class::MOP is right (IMHO) when it claims that some of the things you can do with a meta circular MOP can be hard to do in Ruby (DISCLAIMER: I have zero, zilch, nada Perl knowledge, so I'm thinking Smalltalk like MOP vs CLOS like MOP here) but most of them are very specific (I'm thinking about metaclass instantation here) and there are ways to make things work without them. I think it all depends on your point view, meta circular MOPs are cooler but more on the academic side and non meta circular MOPs are more practical and easier to implement.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文