Skip to content

Instantly share code, notes, and snippets.

@karthiks
Created June 27, 2011 18:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karthiks/1049442 to your computer and use it in GitHub Desktop.
Save karthiks/1049442 to your computer and use it in GitHub Desktop.
Finding the Ruby method definition - one that is invoked at runtime.
class Fixnum
def crime
end
end
p 2.method(:crime)
#<Method: Fixnum#crime> #crime() defined in Fixnum class is invoked
module Loser
def crime
end
end
module Perpetrator
def crime
end
end
class Fixnum
include Loser
include Perpetrator
end
p 2.method(:crime)
#<Method: Fixnum(Perpetrator)#crime> #crime() defined in Perpetrator module is invoked
class Fixnum
def crime
end
end
p 2.method(:crime)
#<Method: Fixnum#crime> #re-defined crime() in Fixnum class is invoked
p 2.method(:crime).source_location #shows file full path and line number #From Ruby 1.9 only
# => ["/home/karthik/MyRubyProjects/practice/katas/ruby_method_method_test.rb", 22]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment