On 6/3/05, Dave Burt <dave / burt.id.au> wrote:
> > 3.  When "whiteout" is required, the original code must be executed with
> > no
> >     change in functionality.
> 
> That means that a file that is just "require 'whiteout'" and then a bunch of
> whitespace should run like the original program before it was whited out.
> 
> Perl has something called "source filters" (see perl doco "perlfilter"),
> which apply a transformation to following source code, kind of like a
> preprocessor. This would be an ideal way of dealing with this situation, and
> avoid doing File.read($0).
> 
> Is there a way to do that in Ruby? Can a C extension concievably alter
> following code before it is parsed? I think I'm expecting a simple "no".
> 
> Cheers,
> Dave
> 
> 
> 
> 

The problem with doing source filters in ruby is that 'use' doesn't
really work the same way as require. Things that are 'use'd get looked
at BEFORE perl reads the rest of the file. This means that this will
let you do things like have invalid syntax before the source filter
gets to it. In contrast, require in ruby (and in perl btw) occurs more
on the runtime side of the compile --- runtime scale. Same thing with
ruby's BEGIN blocks. Which is why perl will let you do this:

$ cat test.pl
#!/usr/bin/env perl
BEGIN { exit 0; }
dasdadadaddada = 33423q3412 -,_1+e=3

$ perl test.pl
$

Even more amusing:
$ perl -c test.pl
test.pl syntax OK

In ruby however...
$ cat test.rb
#!/usr/bin/env ruby
BEGIN { exit 0 }
fsdnddfqw,+-};

$ ruby test.rb
test.rb:2: syntax error
fsdnddfqw,+-};
           ^

You can possibly do it with soemthing like this:
$ cat runaway.rb
exit 0

$ ruby -rrunaway test.rb
$

But to use this to create a source filter requires smarter people than me.