If this sounds like the stupidest _advanced_ question asked in the history of
this newsgroup... well, that's my job.
This page describes how Ruby Node reports Ruby's own parse tree of a method:
http://rubynode.rubyforge.org/
Given a line like...
q = "3.times { puts 'Ruby' }".parse_to_nodes.transform
...is there a method out there that takes q and turns it back into its original
source - "3.times...". The representation doesn't need to be exact! I'm just
asking before I dive in and write one.
--
Phlip
http://www.oreilly.com/catalog/9780596510657/
^ assert_xpath
ParseTree + Ruby2Ruby?
[1] http://rubyforge.org/projects/parsetree/
[2] http://seattlerb.rubyforge.org/ruby2ruby/
[3] http://rubyforge.org/projects/seattlerb
HTH,
--
Luis Lavena
Multimedia systems
-
A common mistake that people make when trying to design
something completely foolproof is to underestimate
the ingenuity of complete fools.
Douglas Adams
Both ruby2ruby and nodewrap support this.
Nodewrap also has some support for decoding YARV bytecode, but needs to
be updated for latest 1.9.
Paul
>> This page describes how Ruby Node reports Ruby's own parse tree of a method:
>>
>> http://rubynode.rubyforge.org/
> ParseTree + Ruby2Ruby?
The great thing about rubynode is you can reach out and squeeze your own
program's live nodes, as they run. I need that, because I'll probably go with
the {} block version of the system I'm considering.
Tx to Paul I'm looking at nodewrap next!
--
Phlip
> Both ruby2ruby and nodewrap support this.
Sorry - gotta use rubynode.
I'm asking this silly question because it seems that traversing the nodes and
emitting Ruby is a solution that only needs to be coded once, so I'm checking if
anyone has already done it before I leap into it.
So... leap!
--
Phlip
I am the author of rubynode. As far as I know there currently is no code
out there that translates RubyNodes back to Ruby.
I thought about implementing that functionality, but I never needed it, so
I never did (and currently don't plan to).
Yes, implementing this should be pretty straightforward.
> So... leap!
Please do ;-)
If you want, I could include your code with RubyNode, once you are done.
If you are interested or need help, you can contact me off-list.
Dominik
> The great thing about rubynode is you can reach out and squeeze your
> own
> program's live nodes, as they run.
You can do that with ParseTree as well. Take a look at pt_testcase.rb
in ParseTree. This is the test suite I use for ParseTree as well as
ruby2ruby. I assure you, you have your work cut out for you.
> If you want, I could include your code with RubyNode, once you are done.
Tx. I will also look at the 1.9 issue.
Here's what I have so far:
def test_reflect_
x = 42
y = 43
assert_equal "x<42> == 42\t--> true" , reflect_{ x == 42 }
assert_equal "42 == x<42>\t--> true" , reflect_{ 42 == x }
assert_equal "y<43> == 41\t--> false", reflect_{ y == 41 }
assert_equal "y<43> >= 41\t--> true", reflect_{ y >= 41 }
assert_equal "41 < y<43>\t--> true" , reflect_{ 41 < y }
assert_equal "not(y<43> == 41)\t--> true", reflect_{ y != 41 }
end
Note the x<42>. Because I can't get the 42 out of the Node (can't I?), I must
fetch it with eval(x, block.binding). That implies if 'x' were a method, it
would evaluate twice, it could go volatile, and it could have side-effects.
reflect_{} is not for everyone - it certainly shouldn't appear inside a
production feature, so I can live with these issues. But they seem rather tacky.
And, yet, the last time Ruby thought about that Node, it was busy evaluating
that 42...
--
Phlip