开发者

How do I see where in the Class Hierarchy a Method was defined and Overridden in Ruby?

开发者 https://www.devze.com 2023-01-12 03:36 出处:网络
Is there a way to know whether or not a method has been overridden by a subclass programmatically?Something that works like this:

Is there a way to know whether or not a method has been overridden by a subclass programmatically? Something that works like this:

class BaseModel
  def create
    puts "superclass"
  end
end

class 开发者_如何学JAVASomeModel < BaseModel
  def create
    puts "subclass"
  end
end

puts SomeModel.overridden_instance_methods #=> [:create]

Any ideas?


SomeModel.instance_methods(false) & BaseModel.instance_methods

The false makes instance_methods not include inherited methods. We then use set intersection to find all the methods that were defined on SomeModel which have previously been defined on BaseModel (or Object).

0

精彩评论

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