From: Ara.T.Howard@... Date: 2005-06-09T23:30:28+09:00 Subject: Re: [SUMMARY] Whiteout (#34) On Thu, 9 Jun 2005, Ruby Quiz wrote: > Does this library have any practical value? Probably not. It's been suggested > in the Perl community that hacks like this are a good minor deterrent to those > trying to read source code you would rather keep hidden, but it must be stressed > that this is no form of serious security. Regardless, it's a fun little toy to > play with. > > It was mentioned in the discussion that Perl, where ACME::Bleach comes from, > includes a framework for source filtering. It can be used to make modules that > modify source code much as we are doing in this quiz. Perl's Switch.pm is a > good example of this, but ironically ACME::Bleach is not. > > That naturally leads to the question, can you build source filters in Ruby? > Clearly we can build ACME::Bleach, but not all source filters are as simple I'm > afraid. Consider this: > > #!/usr/local/bin/ruby -w > > require "fix_my_broken_syntax" > > invalid++ > > Now the thought here is that fix_my_broken_syntax.rb will read my source, change > it so that it does something valid, eval() it, and exit() before the invalid > code is an issue. Here's a trivial example of fix_my_broken_syntax.rb: > > #!/usr/local/bin/ruby -w > > puts "Fixed!" > exit > > Does that work? Unfortunately, no: > > $ ruby invalid.rb > invalid.rb:5: syntax error > invalid++ > ^ > > Ruby never gets to loading the library, because it's not happy with the syntax > of the first file. That makes writing a source filter for anything that isn't > valid Ruby syntax complicated and if it is valid Ruby syntax, you can probably > just code it up in Ruby to begin with. a hack but: harp:~ > cat fix_my_broken_syntax.rb src = open($0).read src.gsub! %r/([_a-z][_a-zA-Z]*)\+\+/, '((\1+=1;\1 - 1))' eval src exit harp:~ > cat a.rb #!/usr/local/bin/ruby -r./fix_my_broken_syntax.rb n = 41 p n++ p n harp:~ > ./a.rb 41 42 cheers. -a -- =============================================================================== | email :: ara [dot] t [dot] howard [at] noaa [dot] gov | phone :: 303.497.6469 | My religion is very simple. My religion is kindness. | --Tenzin Gyatso ===============================================================================