matz / zetabits.com (Yukihiro Matsumoto) writes:

> Hi,
> 
> In message "[ruby-talk:04481] Invoking an extension after compilation"
>     on 00/08/16, Dave Thomas <Dave / thomases.com> writes:
> 
> |I want to write a C extension module which runs automatically after a
> |source file has been read, but before it executes. I tried putting the 
> |following in the Init_ function:
> |
> |  code = NEW_CALL(NEW_CONST(rb_intern("Stuff")), rb_intern("doIt"), 0);
> |  
> |  ruby_eval_tree_begin = block_append(ruby_eval_tree_begin, code);
> |
> |
> |However, this didn't work, because require_libraries in ruby.c saves
> |and restores the value in ruby_eval_tree_begin.
> 
> I'm not sure what did you want to do, but I guess
> 
>   rb_eval_string("Stuff.doIt");
> 
> or
> 
>   rb_funcall(rb_cStuff, rb_intern("doIt"), 0);
> 
> at the end of Init_ function may work for you.

The problem is that at that point the rest of the program hasn't been
compiled.

What I'm trying to do is mess around with a program's parse tree, so I
need ruby_eval_tree to be set up with the main program's parse. I'd
like to be able to do this without changing that main program, so you
could say something like:

   ruby -rStuff  prog.rb

and have 'Stuff' be able to examine prog.rb's parse tree.

However, the Init_ function is executed before prog.rb is loaded in,
so I can't get to ruby_eval_tree at this point. I _could_ do it with

   ruby -rprog.rb -rStuff -e1

but then it actually executes 'prog.rb' before running my library.


Does this make sense?


Dave