須藤です。

In <20080319.234924.545103554540142144.kou / cozmixng.org>
  "[ruby-dev:34093] 拡張ライブラリ初期化中でのmodule_eval" on Wed, 19 Mar 2008 23:49:19 +0900,
  Kouhei Sutou <kou / cozmixng.org> wrote:

> 拡張ライブラリを初期化しているとき(Init_XXXを呼んでいるとき)
> にmodule_evalを呼ぶとトップレベルで評価されている気がします。
> 
> a.c:
>   #include <ruby.h>
> 
>   void
>   Init_a(void)
>   {
>       rb_funcall(rb_cArray, rb_intern("module_eval"),
>                  1, rb_str_new2("def xxx; yyy; end"));
>   }
> 
> extconf.rb:
>   require 'mkmf'
> 
>   create_makefile("a")
> 
> 実行例:
>   % ruby19 extconf.rb
>   % make
>   % ruby19 -v -r a -e 'xxx'
>   ruby 1.9.0 (2008-03-19 revision 0) [x86_64-linux]
>   (eval):1:in `xxx': undefined local variable or method `yyy' for main:Object (NameError)
>           from -e:1:in `<main>'
> 

これで動くようにはなります。


Index: eval.c =================================================================== --- eval.c (revision 15824) +++ eval.c (working copy) @@ -1855,8 +1855,8 @@ cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp); } - stored_cref = (NODE *)vm_cfp_svar_get(th, cfp, 2); - vm_cfp_svar_set(th, cfp, 2, (VALUE)vm_cref_push(th, under, NOEX_PUBLIC)); + stored_cref = vm_set_special_cref(th, cfp->lfp, + vm_cref_push(th, under, NOEX_PUBLIC)); PUSH_TAG(); if ((state = EXEC_TAG()) == 0) { @@ -1865,7 +1865,7 @@ POP_TAG(); /* restore environment */ - vm_cfp_svar_set(th, cfp, 2, (VALUE)stored_cref); + vm_set_special_cref(th, cfp->lfp, stored_cref); pcfp->self = stored_self; if (state) {