From: nobu.nokada@... Date: 2003-01-26T21:57:18+09:00 Subject: Re: datafiles as Ruby source musings Hi, At Sun, 26 Jan 2003 06:18:09 +0900, Wilbert Berendsen wrote: > module Data > load "datafile" > end Kernel#load code in the file into toplevel context if (omitted) second argument is false, or an anonymous module. If it were return that anonymous module, you can write as: module Data include load("datafile", true) end or even Data = load("datafile", true) > 2. > > Other files which my app will need, must contain Makefile-like structures. > I would rather just "load" the file, and having the Ruby interpreter take care > of the parsing (example:) > > ALSA && ( > depend "audio/alsa-driver" { |p| p.version >= 0.9 } > configure << "--enable-alsa" > ) > > where 'depend' and 'configure' are just methods in the current instance, and > 'ALSA' a constant defined in a module that got included. But again using > 'obj.instance_eval File.new("file").read' is the only thing I can think of. To do it, Kernel#load would need to accept a Module as second argument, like as: load("depend", Data) Although I don't guess this modification rarely breaks existing code, but not sure it's a good thing. Index: eval.c =================================================================== RCS file: /cvs/ruby/src/ruby/eval.c,v retrieving revision 1.389 diff -u -2 -p -r1.389 eval.c --- eval.c 23 Jan 2003 08:27:05 -0000 1.389 +++ eval.c 26 Jan 2003 12:44:42 -0000 @@ -5398,8 +5398,7 @@ rb_mod_module_eval(argc, argv, mod) VALUE rb_load_path; -void +VALUE rb_load(fname, wrap) - VALUE fname; - int wrap; + VALUE fname, wrap; { VALUE tmp; @@ -5411,5 +5410,5 @@ rb_load(fname, wrap) TMP_PROTECT; - if (wrap && ruby_safe_level >= 4) { + if (RTEST(wrap) && ruby_safe_level >= 4) { StringValue(fname); } @@ -5428,5 +5427,6 @@ rb_load(fname, wrap) wrapper = ruby_wrapper; ruby_cref = top_cref; - if (!wrap) { + if (!RTEST(wrap)) { + wrap = Qnil; rb_secure(4); /* should alter global state */ ruby_class = rb_cObject; @@ -5434,6 +5434,9 @@ rb_load(fname, wrap) } else { - /* load in anonymous module as toplevel */ - ruby_class = ruby_wrapper = rb_module_new(); + if (TYPE(wrap) != T_MODULE) { + /* load in anonymous module as toplevel */ + wrap = rb_module_new(); + } + ruby_class = ruby_wrapper = wrap; self = rb_obj_clone(ruby_top_self); rb_extend_object(self, ruby_class); @@ -5485,10 +5488,11 @@ rb_load(fname, wrap) if (!NIL_P(ruby_errinfo)) /* exception during load */ rb_exc_raise(ruby_errinfo); + return wrap; } -void +VALUE rb_load_protect(fname, wrap, state) VALUE fname; - int wrap; + VALUE wrap; int *state; { @@ -5497,8 +5501,9 @@ rb_load_protect(fname, wrap, state) PUSH_TAG(PROT_NONE); if ((status = EXEC_TAG()) == 0) { - rb_load(fname, wrap); + wrap = rb_load(fname, wrap); } POP_TAG(); if (state) *state = status; + return wrap; } @@ -5511,6 +5516,5 @@ rb_f_load(argc, argv) rb_scan_args(argc, argv, "11", &fname, &wrap); - rb_load(fname, RTEST(wrap)); - return Qtrue; + return rb_load(fname, wrap); } Index: intern.h =================================================================== RCS file: /cvs/ruby/src/ruby/intern.h,v retrieving revision 1.109 diff -u -2 -p -r1.109 intern.h --- intern.h 16 Jan 2003 07:34:01 -0000 1.109 +++ intern.h 26 Jan 2003 12:39:37 -0000 @@ -168,6 +168,6 @@ ID rb_frame_last_func _((void)); VALUE rb_obj_instance_eval _((int, VALUE*, VALUE)); VALUE rb_mod_module_eval _((int, VALUE*, VALUE)); -void rb_load _((VALUE, int)); -void rb_load_protect _((VALUE, int, int*)); +VALUE rb_load _((VALUE, VALUE)); +VALUE rb_load_protect _((VALUE, VALUE, int*)); NORETURN(void rb_jump_tag _((int))); int rb_provided _((const char*)); -- Nobu Nakada