开发者

Does ruby provide a method to show the hierarchy calls?

开发者 https://www.devze.com 2022-12-09 10:13 出处:网络
That\'s all, i want to see what are the clases that inherits a fixed class. There is a method for this in Ruby?

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


Are you asking to see all the ancestors of a class, or the descendants? For ancestors, use:

Class.ancestors

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:

ObjectSpace.each_object(Class) do |klass| 
  p klass if klass < StandardError
end

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

Example:

class Foo
end

class Bar < Foo
end

Bar.ancestors # => [Bar, Foo, Object, Kernel]
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号