How to discover the overrided methods in Ruby/Rails?

For instance methods there is an Object::method_added(name) method you can override, similar to 'inherited' you have used: class test def self. Method_added(name) puts "method_added(#{name. Inspect})" super end end irb(main):002:0> class Child nil You can then compare a received name to a list of your methods: Test.

Instance_methods. Include?(name. To_s) With class methods this approach does not work (even if you do things like class Singleton_method_added(name) puts "Class method added #{name.

Inspect}" end end This is only the first part of the problem, because you need to know which class defined the method (it will be self) and whether the method is a new one, or overridden one. Experiment with this code: class Test def self. Singleton_method_added(name) if self == Test puts "My own class method added: #{self.

Name}. #{name. Inspect}" elsif Test.

Methods(false). Include?(name. To_s) puts "Class method overriden: #{self.

Name}. #{name. Inspect}" elsif Test.

Methods(true). Include?(name. To_s) puts "My parent's class method overriden: #{self.

Name}. #{name. Inspect}" else puts "New class method added: #{self.

Name}. #{name. Inspect}" end end end.

That's exactly what I needed! Thanks a lot Arsen7! Unfortunately I don't have enough reputation (15) here to vote you up... =/ – Rodrigo Souto Nov 18 '10 at 13:40 Nevermind!

I am happy I could help ;) – Arsen7 Nov 22 '10 at 14:05.

Maybe a first step to the solution: By calling child. Instance_method(:bar) (if child refers to the class) or child. Method(:bar) (if it refers to an instance of Child) you can get an UnboundMethod or Method object representing your method: a = Test.

Instance_method(:foo) be = Child. Instance_method(:foo) Unfortunately, a == be evaluates to false, although both refer to the same method.

My mistake. I forgot to put the 'self. ' before all methods definitions.

They are all class methods. So, I tried what you said (using child. Method(:bar) and child.

Method(:foo)) and both executed without UnboundMethod. I guess it goes without mistake to foo method because he uses the super class method. – Rodrigo Souto Nov 17 '10 at 13:34.

Def overridden_methods klass = self. Class klass. Instance_methods.

Select {|m| klass. Instance_method(m). Owner == klass} end Change according to your needs.

Thanks Tass, but as I said before, I was trying to catch the class method not instance methods. Arsen found the way to solve the problem up here. – Rodrigo Souto Nov 18 '10 at 13:41 s/instance_method/method and you're good to go.

– Tass Nov 19 '10 at 13:23.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions