From: Jim Weirich Date: 2005-08-12T14:33:11+09:00 Subject: Re: [ANN] LibInject 0.1.0: Small Pieces Smooshed Together On Thursday 11 August 2005 11:31 pm, Francis Hwang wrote: > I've just released the first version of LibInject, which is a developer > tool for injecting external dependencies into a Ruby file. More > specifically, it finds require statements that point to files that > aren't in the standard library, expands those files, and drops the raw > text into the original file. > > http://libinject.rubyforge.org/ > > Why would somebody do this instead of just using a plain old require > statement? One reason might be because you want to write a one-file > script that can also have external dependencies. I wrote LibInject for > use with FeedBlender, which is intended as a one-file script aimed at > Ruby newbies who might not even know how to install from .tar.gz files > or from RubyGems. With LibInject, I can have external dependencies in > FeedBlender, use a Rake task to inject those libraries into > feedblender.rb, and then distribute the end result. > > For example, let's say you've three files, a.rb, b.rb, and c.rb. a.rb > requires b.rb and c.rb, and b.rb requires c.rb: > > a.rb: > > require 'b' > require 'c' > > class A; end > > b.rb: > > require 'c' > > class B; end > > c.rb: > > class C; end > > If you wanted to distribute a version of a.rb without any external > dependencies, this is how you'd run LibInject: > > require 'libinject' > > puts LibInject.lib_inject( File.open( 'a.rb' ) ) > > # We could also pass LibInject.lib_inject a String, like so: > # contents = File.open( 'a.rb' ) do |f| f.gets( nil ); end > # puts LibInject.lib_inject( contents ) > > And this is what you'll get: > > # > v---------v---------v---------v---------v---------v---------v---------v > # LibInject: begin 'b' library injection > # > v---------v---------v---------v---------v---------v---------v---------v > > > # > v---------v---------v---------v---------v---------v---------v---------v > # LibInject: begin 'c' library injection > # > v---------v---------v---------v---------v---------v---------v---------v > > class C; end > > # > ^---------^---------^---------^---------^---------^---------^---------^ > # LibInject: end 'c' library injection > # > ^---------^---------^---------^---------^---------^---------^---------^ > > > class B; end > > # > ^---------^---------^---------^---------^---------^---------^---------^ > # LibInject: end 'b' library injection > # > ^---------^---------^---------^---------^---------^---------^---------^ > > > > class A; end > > The code looks sort of messy, but now it's all in one file. > > Anyway, it's a strange little script, and I'm curious as to whether > anybody else will get some use out of it. Let me know if you do! > > F. Cool! Does it handle things like this: class Command def create_option_parser require 'optparse' @parser = OptionParser.new ... end end -- -- Jim Weirich jim@weirichhouse.org http://onestepback.org ----------------------------------------------------------------- "Beware of bugs in the above code; I have only proved it correct, not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)