I would like to know whether I can get source code a method on the fly, and whether I can get which file is this method in.
like
A.new.method(:a).SOURCE_CODE
A.new.method(:a).FILE
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Use
source_location:Note that for builtin methods,
source_locationreturnsnil. If want to check out the C source code (have fun!), you’ll have to look for the right C file (they’re more or less organized by class) and find therb_define_methodfor the method (towards the end of the file).In Ruby 1.8 this method does not exist, but you can use this gem.