Ruby 是否有元对象协议?如果没有,是否可以实现一个?
请原谅我的无知,但是什么是元对象协议?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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
什么是元对象协议?
我遇到的最好的描述来自
Class::MOP
文档: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: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.如果您仔细查看定义,您会发现 Ruby 确实有 MOP。是不是像CLOS里的那样?不,CLOS 是一个元循环 MOP,它很棒(我什至可以说是天才),但它不是唯一真正的方法,请看一下 Smalltalk。要实现(假设是基本的)MOP,您需要的只是提供允许运行时执行以下操作的函数:
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:
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.