Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I find where a ruby method is declared?

I have a ruby method (deactivate!) that is on an activeRecord class. However, I can't seem to find where that method is declared.

There have been numerous developers on this project, so it could be anywhere. There is a deactivate! on an unrelated class, but it doesn't seem to get called.

Any ideas how to find all the superclasses for an instace, or where to find the code for deactivate!?

like image 277
Joe Fair Avatar asked Apr 13 '10 15:04

Joe Fair


People also ask

Where method is defined Ruby?

In Ruby the method for object can be defined from many places: modules, inheritance, meta-programming, other language's extensions and etc. Imagine that you have installed a lot of gems in your application and every gem potentially can define or redefine method on any object.

Where does method come from?

early 15c., "regular, systematic treatment of disease," from Latin methodus "way of teaching or going," from Greek methodos "scientific inquiry, method of inquiry, investigation," originally "pursuit, a following after," from meta "in pursuit or quest of" (see meta-) + hodos "a method, system; a way or manner" (of ...


1 Answers

First question would be: is it an actual method? Does obj.method(:deactivate!) raise an error?

If it doesn't, then you can use Method#source_location(in Ruby 1.9 only, and backports can't support it):

obj.method(:deactivate!).source_location

If it does raise a NoMethodError, it is handled via method_missing. This makes it hard to track. If it accepts arguments, I'd try sending the wrong type and using the backtrace of the raised exception.

Are you using state_machine? If you have an event transition called :deactivate, the model will have the method #deactivate! created automatically.

like image 103
Marc-André Lafortune Avatar answered Sep 22 '22 06:09

Marc-André Lafortune