Whoa, SCRIPT_LINES__?! #
Did you know this? I had no idea. If you define SCRIPT_LINES__
constant as a Hash, then the source code for all load
‘d or require
‘d files will be stored in the Hash! The key is the path to the source file and the value is an array of strings, each a line of source code.
>> SCRIPT_LINES__ = {} >> require 'drb' >> SCRIPT_LINES__.keys => ["/usr/local/lib/ruby/1.8/drb/drb.rb", "/usr/local/lib/ruby/1.8/drb/eq.rb", "/usr/local/lib/ruby/1.8/drb/invokemethod.rb", "/usr/local/lib/ruby/1.8/thread.rb", "/usr/local/lib/ruby/1.8/drb.rb"] >> SCRIPT_LINES__['/usr/local/lib/ruby/1.8/drb.rb'] => ["require 'drb/drb'\n", "\n"]
WEIRD. What gives with the trailing underscores? It’s such a big constant that it leaves a two-character shadow!!
Note that $SAFE
must be zero. See yycompile() in parse.y for details. But, yes, I saw this in PickAxe the Second.
bleh
SCRIPT_LINES__
is what makes coverage, coverage2 and rcov work…cilibrar
Hey this “coverage2” is nice; I just tried it and it worked fine first try on my program. What a great utility! n.b. it did give me a warning: /home/webuser/mySoftware/ruby-1.8.2/lib/ruby/site_ruby/1.8/coverage.rb:264: warning: regexp has `}’ without escape
cilibrar
I think the SCRIPT _LINES hash thing is super-powerful and I think it could be used to make an excellent remove-one-line style automated tester; In this type of system, a new copy of the entire source tree of a program is made with one line removed. Then the tests are run; if ruby fails with an error code or a test fails, then this line “passes”. If neither of those events happen, then this line is flagged as “potentially redundant”. (if it’s a comment or whitespace it would probably be ignored) This process is repeated all over for each line in the entire source base. This is an interesting way to automatically detect unecessary code in some common cases. It is very slow e.g. perhaps O(n*n) in number of lines of code, but very interesting.
Comments are closed for this entry.