Getting Source of Class or Method

Hi Guys,

Recently I’ve started playing with jrubyparser to check how far I can
go towards implementing AST transformations (similar to what Groovy
has). JRubyParser provides a nice api and it’s fast enough. The only
problem is finding a place to perform AST transformations. I started
with implementing my own load that reads a file, transforms it using
JRubyParser and then evals it. Right now I’m trying to find a better
way to do it. That’s why I’m curious:

  1. Is there a moment when jruby has already read a file but haven’t
    evaluated it yet? And is there a callback or similar extension point?
    Basically, is there a way to change what was read from the file before
    it was evaluated?

  2. Another way to do all these transformations would be to get the
    source code of a method and transform it. Is there a way to get the
    source code of a method or a class at runtime?

Cheers,
Victor

you can kind of kludgey hack-zy get the source by using the
source_location gem then reparsing the file.
The gist link at the end of this:

might be interesting. Sorry I don’t know the “real” way at all.

Thanks for your response, Roger. I’ll give it a try.

Thank you,
Victor.

sourcify and/or GitHub - ConradIrwin/method_source: return the sourcecode for a method may be
useful

I failed on responding to this. Sorry!

On Mon, Feb 27, 2012 at 8:50 PM, Victor S. [email protected]
wrote:

Hi Guys,

Recently I’ve started playing with jrubyparser to check how far I can
go towards implementing AST transformations (similar to what Groovy
has). JRubyParser provides a nice api and it’s fast enough. The only
problem is finding a place to perform AST transformations. I started
with implementing my own load that reads a file, transforms it using
JRubyParser and then evals it. Right now I’m trying to find a better
way to do it. That’s why I’m curious:

I’d love to see an AST transformation capability in JRubyParser. There
is already some transformation logic in the JRubyParser library under
the refactoring stuff, but I’m not sure whether it’s working right
now. I did play with it a bit recently, and it’s at least partially
functional.

  1.  Is there a moment when jruby has already read a file but haven't
    

evaluated it yet? And is there a callback or similar extension point?
Basically, is there a way to change what was read from the file before
it was evaluated?

There’s no callback for that point in execution, but there easily
could be. Code is passed to the parser, and the parser passes back a
Node for us to interpret or compile. That could easily be hooked to
allow code to manipulate it.

I’d love tos ee something added.

  1.  Another way to do all these transformations would be to get the
    

source code of a method and transform it. Is there a way to get the
source code of a method or a class at runtime?

I’d also love to see a way to get the source code of a given method :slight_smile:
I don’t think it would be hard, and it would be very useful.

These would be good projects for someone to work on :slight_smile:

  • Charlie