Read more

Ruby: Finding where a method is defined

Dominik Schöler
March 20, 2013Software engineer at makandra GmbH

Dead simple: Get the method object and ask for its owner:

"foo".method(:upcase)
=> #<Method: String#upcase> 
"foo".method(:upcase).owner
=> String
Illustration online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
Read more Show archive.org snapshot

Ruby 1.9 adds a method Method#source_location that returns file and line number where that method is defined.

class Example; def method() end end
=> nil
Example.new.method(:method).source_location
=> ["(irb)", 11] 

"foo".method(:upcase).source_location
=> nil # String#upcase is a native method that's defined in C

Rumor has it in Ruby 1.8 there is a gem ruby18_source_location that does something similar.


Also see

Posted by Dominik Schöler to makandra dev (2013-03-20 10:28)