Is it possible to convert a proc object or block to a string of Ruby code?

Is it possible to convert a proc object or block to a string of Ruby code?

Thanks, Michael

Matz does not hold the source code in memory so…no you cannot (unless
you did your own loading of the proc from eval’ing a string and then
keeping a handle to it.)

-rich

···

-----Original Message-----
From: Michael Davis [mailto:mdavis@sevasoftware.com]
Sent: Friday, December 06, 2002 1:36 PM
To: ruby-talk ML
Subject: Is it possible to convert a proc object or block to
a string of Ruby code?

Is it possible to convert a proc object or block to a string
of Ruby code?

Thanks, Michael

In article 3DF0EE1F.1010003@sevasoftware.com,

···

Michael Davis mdavis@sevasoftware.com wrote:

Is it possible to convert a proc object or block to a string of Ruby code?

Thanks, Michael

Talk to ts (decoux@moulon.inra.fr) he may have something for you.

Phil

“Or perhaps the truth is less interesting than the facts?”
Amy Weiss (accusing theregister.co.uk of engaging in ‘tabloid journalism’)
Senior VP, Communications
Recording Industry Association of America

Matz does keep nodes for line numbers, though. So one (very ugly)
possibility would be to:

  • Find the first node for the proc
  • Find the first NODE_NEWLINE; that should contain the line number the
    proc starts on.
  • Open the file that node points to and start reading.

This will let you get a string from a proc, but procs contain a lot more
state than just the nodes that need to be evaluated. Saving this state
and reloading it would be an interesting exercise, to say the least.

Paul

···

On Sat, Dec 07, 2002 at 04:54:08AM +0900, Rich Kilmer wrote:

Matz does not hold the source code in memory so…no you cannot (unless
you did your own loading of the proc from eval’ing a string and then
keeping a handle to it.)

Indeed, for it would allow/need/overlap the following projects:

  • RubyInRuby parsers and AST dumpers, if the code is saved as an AST
  • Rite: if bytecode is used
  • distributing ruby apps as a single executable
  • image-based Ruby (à la Smalltalk): all the context could be dumped by
    a “top level proc”
  • distributed Ruby: yet another way to do it

everything from the top of my head, but there’s for sure many more…

···

On Sat, Dec 07, 2002 at 06:03:46AM +0900, Paul Brannan wrote:

On Sat, Dec 07, 2002 at 04:54:08AM +0900, Rich Kilmer wrote:

Matz does not hold the source code in memory so…no you cannot (unless
you did your own loading of the proc from eval’ing a string and then
keeping a handle to it.)

Matz does keep nodes for line numbers, though. So one (very ugly)
possibility would be to:

  • Find the first node for the proc
  • Find the first NODE_NEWLINE; that should contain the line number the
    proc starts on.
  • Open the file that node points to and start reading.

This will let you get a string from a proc, but procs contain a lot more
state than just the nodes that need to be evaluated. Saving this state
and reloading it would be an interesting exercise, to say the least.


_ _

__ __ | | ___ _ __ ___ __ _ _ __
'_ \ / | __/ __| '_ _ \ / ` | ’ \
) | (| | |
__ \ | | | | | (| | | | |
.__/ _,
|_|/| || ||_,|| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

Turn right here. No! NO! The OTHER right!