0

Do u guys know if it is possible to learn what class the method is defined in?

For example,

to_a is probably defined in Enumerable and in Numeric or elsewhere. Is there a function, like with class objects:

=> aaa=[1,2,3,4,5]
=> aaa.class
=> Array

Is there a method like "defined_in?" to learn which class a method is defined in?

=> to_a.defined_in?
Viktor Kaufman
  • 137
  • 1
  • 9

1 Answers1

0

On Ruby 1.9 and up, you can use Method#source_location. On Ruby 1.8, there is a gem ruby18_source_location that backports this functionality.

See https://stackoverflow.com/a/13015691/260122 and the other answers to the same question for a good discussion of further ways to analyze source code, including the debugger.


Edit: Sorry, I misread your question. To find out what class or module defined a method, use Method#owner. As in:

> %w(a a a).method(:to_a).owner
=> Array
Community
  • 1
  • 1
clacke
  • 7,688
  • 6
  • 46
  • 48