ruby 是否提供了显示层次结构调用的方法?
就这样,我想看看继承固定类的类有哪些。 Ruby 中有这样的方法吗?
Aptana 提供了一个选项来显示这一点,但是有什么方法吗?
谢谢
That's all, i want to see what are the clases that inherits a fixed class. There is a method for this in Ruby?
Aptana offers an option that shows this, but is there any method?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你是要求查看一个类的所有祖先,还是后代?对于祖先,请使用:
然而,对于后代,没有类似的“开箱即用”方法。您可以使用 ObjectSpace,如下所示,但它速度很慢,并且可能无法跨 Ruby 实现移植:
编辑:
也可以使用 Class#inherited 挂钩来跟踪子类化。但是,这不会捕获在定义跟踪功能之前创建的任何子类,因此它可能不适合您的用例。但是,如果您需要在应用程序内定义的类上以编程方式使用该信息,那么这可能是正确的选择。
Are you asking to see all the ancestors of a class, or the descendants? For ancestors, use:
There is no comparable method "out of the box" for descendants, however. You can use ObjectSpace, as below, but it's slow and may not be portable across Ruby implementations:
EDIT:
One can also use the Class#inherited hook to track subclassing. This won't catch any subclasses created before the tracking functionality is defined, however, so it may not fit your use case. If you need to use that information programmatically on classes defined inside your application, however, this may be the way to go.
Module#ancestors
示例:
Module#ancestors
Example: