Runtime access to the source code of a block?

Is there a way, through native Ruby code or through C extensions, to access
(presumably through a String) the original source of a block object? The
context for this idea is an interactive modeling application, where a user
writes a base model in Ruby, using blocks to encode the behavior of certain
objects in the system, then uses a GUI that allows interactive editing of
these blocks during a run of the model so that behavior can be changed on
the fly. I’d prefer to have the base source be “live” Ruby code and not
strings that are eval’ed.

Thanks,

  • jeff

Jeff wrote:

Is there a way, through native Ruby code or through C extensions, to access
(presumably through a String) the original source of a block object?
Nah, I read a thread earlier this morning, Matsumoto said he left out block
introspection to keep the language from being too weird.

Hello Jeff,

Wednesday, October 30, 2002, 8:43:10 PM, you wrote:

Is there a way, through native Ruby code or through C extensions, to access
(presumably through a String) the original source of a block object? The
context for this idea is an interactive modeling application, where a user
writes a base model in Ruby, using blocks to encode the behavior of certain
objects in the system, then uses a GUI that allows interactive editing of
these blocks during a run of the model so that behavior can be changed on
the fly. I’d prefer to have the base source be “live” Ruby code and not
strings that are eval’ed.

i think you need method to COMPILE string into block, like:

block = compile(“{|x| x*2}”)
p block.call(2) #=>4

···


Best regards,
Bulat mailto:bulatz@integ.ru

Bulat Ziganshin wrote:

i think you need method to COMPILE string into block, like:

block = compile(“{|x| x*2}”)
p block.call(2) #=>4

Doesn’t eval suffice for this?

As for a potential hack for the OP, what about something like this:
class X
def first_definition( &binding )
@binding ||= binding
source = binding.call
redefine( source )
end
def redefine( source )
@source = source
@block = eval “Proc.new { #{source} }”, @binding
end
def x(i)
@block.call(i)
end
end
x = X.new
a = 3
x.first_definition { %{|i|a+i} }
puts x.x(2) #=> 5
x.redefine %{|i|a-i}
puts x.x(2) #=> 1

Ugly enough syntax to know that there is some bad mojo going on

atleast :slight_smile:

···


([ Kent Dahl ]/)_ ~ [ http://www.stud.ntnu.no/~kentda/ ]/~
))_student
/(( _d L b_/ NTNU - graduate engineering - 5. year )
( __õ|õ// ) )Industrial economics and technological management(
_
/ö____/ (_engineering.discipline=Computer::Technology)