From: Simon Strandgaard Date: 2004-03-24T01:09:30+09:00 Subject: Re: rubygems working with non-gems libs? On Tue, 23 Mar 2004 16:02:21 +0100, Simon Strandgaard wrote: > On Tue, 23 Mar 2004 15:40:19 +0100, Raphael Bauduin wrote: > [snip] >> I had to add this before to make it find log4r: >> require "rubygems" >> require_gem "log4r" >> >> Does that mean that if I start using rubygems, I'll have to edit >> ruby code not based on gems? > > > Place this file within Ruby's path and set the env variable. [snip] > > Then you should be able to do it. Ok.. I have cleaned up the require-hack code a little bit. remember to do setenv RUBYOPT "rhack" here is the code server> ruby test_hack.rb Loaded suite TestHack Started ..... Finished in 0.071033 seconds. 4 tests, 5 assertions, 0 failures, 0 errors server> expand -t2 test_hack.rb require 'test/unit' require 'stringio' require 'hack' class TestHack < Test::Unit::TestCase def test_loaderror e = assert_raise(LoadError) do require 'nonexisting' end assert_match(/^No such file to load -- nonexisting$/, e.message) end def test_old_require require 'observer' # is shipped with Ruby end def test_gem_require require 'progressbar' # is shipped with rubygems end def capture_stderr(&block) e = StringIO.new $stderr = e block.call return e.string ensure $stderr = STDERR end def test_failed_loadgem path = $:.clone $:.clear output = capture_stderr do e = assert_raise(LoadError) do require 'progressbar' # is shipped with rubygems end assert_match(/^No such file to load -- progressbar$/, e.message) end assert_match(/Hack/, output) ensure $:.concat(path) end end if $0 == __FILE__ require 'test/unit/ui/console/testrunner' Test::Unit::UI::Console::TestRunner.run(TestHack) end server> expand -t2 hack.rb module Kernel alias old_require require def require(filename) begin old_require(filename) rescue LoadError => old_e begin old_require("rubygems") rescue LoadError $stderr.puts <<-MSG.gsub(/^\s*/, "") Hack which loads "rubygems" when Ruby starts up, failed to find "rubygems". You may want to either install "rubygems" or remove this hack from the RUBYOPT envvar. MSG raise old_e end begin require_gem(filename) rescue LoadError raise old_e end end end end # module Kernel server> -- Simon Strandgaard