Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine the class from which a specified method originated?

Tags:

ruby

I got this question from this discussion. A method call like object.m does not always mean the class of "object" has a "m" method, just like the find method to a Array object is not directly originated from Array object, but from the mixed-in Enumerable module. My question is, given a method, how can we determine the class from which the method originated?

like image 471
eric2323223 Avatar asked Jan 22 '09 03:01

eric2323223


Video Answer


1 Answers

Any class/object method is an object in Ruby, and has some methods of it's own.

So you can do this:

[].method(:count).inspect
=> "#<Method: Array#count>"

[].method(:detect).inspect
=> "#<Method: Array(Enumerable)#detect>"

Quick bit of RegEx and you're done.

like image 135
Toby Hede Avatar answered Oct 20 '22 18:10

Toby Hede