I'd like to write a Ruby code analyzer. I know that we can get methods from classes using metaprogramming, but is there some easy way to get a method's body and analyze it? Is it possible in Ruby? Of course I can just analyze the file line by line, but I'm searching for something easier.
Asked
Active
Viewed 39 times
0
-
1You can use [`Method#source_location`](https://ruby-doc.org/core-2.6.5/Method.html#method-i-source_location) to find out in what file and on what line the method is defined. However I feel like there might be a better way to get the method body, although I'm not aware of one. – 3limin4t0r Oct 17 '19 at 21:51
-
Ruby doesn't keep the original source like JavaScript does. You sort of have to dig it up yourself. – tadman Oct 17 '19 at 22:39
-
I have voted to close this question because it is vague and overly-broad. – Cary Swoveland Oct 18 '19 at 03:21