On Nov 8, 1:10 pm, Randy Kramer <rhkra... / gmail.com> wrote:
> On Thursday 08 November 2007 11:25 am, furtive.cl... / gmail.com wrote:
>
> > As explained in the links I gave, the problem is due to the
> > determination of local variables at compile time.  Really all that is
> > needed is a way to insert code before parsing, like perl's BEGIN block.
>
> Ruby has a BEGIN {} (and END) block as well--have you tried that?  (pickaxe2
> pg. 318)
>

BEGIN {
   eval "foo = 99"
}
puts foo

=> test.rb:6: undefined local variable or method `foo' for main:Object
(NameError)

I realized after posting this that BEGIN wasn't the right analogy as
it still executes too late, after parsing is already done.  Literal
code needs to be inserted into the buffer before parsing, something
like a C-preprocessor #include.

The original motivation was to have a config file for my Rakefile.  An
ugly workaround is to move the content into Rakefile.main and make a
one-liner Rakefile:

eval(File.open("Rakefile.config") { |f|
        f.read
     } + "\n" +
     File.open("Rakefile.main") { |f|
        f.read
     })