Hi,
I wanted to try SDS (http://www.starware.one.pl/software/sds/index.html).
It depends on log4r, and as log4r is available as a gem, it convinced me to try
rubygems.
Installing log4r with rubygems was really easy.
However, sds didn’t seem to find log4r on my system.
It complained about not finding log4r/configurator. Here is the code in the file sds.rb
require "log4r/configurator"
require “log4r”
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?
Raph
PS: from the doc (http://rubygems.rubyforge.org/wiki/wiki.pl?UserDoc), I thought gems would go into
/usr/local/lib/ruby/gems/1.8
but the went into
/usr/lib/ruby/gems/
[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.
setenv RUBYOPT “rgemreq”
expand -t2 gemreq.rb
module Kernel
alias old_require require
def require(filename)
begin
old_require(filename)
rescue LoadError
old_require(“rubygems”)
require_gem(filename)
end
end
end # module Kernel
Then you should be able to do it.
···
On Tue, 23 Mar 2004 15:40:19 +0100, Raphael Bauduin wrote:
–
Simon Strandgaard
New SDS version (to be release in a few weeks) will be probably
available as a gem too, so your problem will soon be solved.
···
On Tue, 23 Mar 2004 15:40:19 +0100, Raphael Bauduin wrote:
Hi,
I wanted to try SDS (http://www.starware.one.pl/software/sds/index.html).
It depends on log4r, and as log4r is available as a gem, it convinced me to try
rubygems.
Installing log4r with rubygems was really easy.
However, sds didn’t seem to find log4r on my system.
It complained about not finding log4r/configurator. Here is the code in the file sds.rb
require “log4r/configurator”
require “log4r”
I had to add this before to make it find log4r:
require “rubygems”
require_gem “log4r”
–
Marek Janukowicz
[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
ruby test_hack.rb
Loaded suite TestHack
Started
…
Finished in 0.071033 seconds.
4 tests, 5 assertions, 0 failures, 0 errors
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
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
···
On Tue, 23 Mar 2004 16:02:21 +0100, Simon Strandgaard wrote:
On Tue, 23 Mar 2004 15:40:19 +0100, Raphael Bauduin wrote:
–
Simon Strandgaard
I have refined the gem-require hack a bit more.
Place the ‘gemrequirehack.rb’ in rubys path.
Add the following line to your tcshrc:
setenv RUBYOPT “rgemrequirehack”
The source-code is here:
expand -t2 gemrequirehack.rb
module Kernel
alias old_require require
def require(filename, version=nil)
ok = nil
begin
ok = old_require(filename)
if version
$stderr.puts <<-MSG.gsub(/^\s*/, “”)
Hack which loads “rubygems” when Ruby starts up, has
been supplied a version string (#{version}), however
the required file (#{filename}) is not a rubygem, and
thus doesn’t take a version number.
The version number has therefore been ignored.
MSG
end
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
ok = require_gem(filename, version || “> 0.0.0”)
rescue LoadError => gem_e
raise gem_e
#raise old_e
end
end
ok
end
end # module Kernel
There is a small testsuite here:
expand -t2 test_gemrequirehack.rb
purpose:
exercise if rubygems require hack is working.
···
depends on:
* rubygems
* progressbar = 0.0.3, shipped with rubygems
require ‘test/unit’
require ‘stringio’
require ‘gemrequirehack’
class TestGemRequireHack < Test::Unit::TestCase
def capture_stderr(&block)
e = StringIO.new
$stderr = e
block.call
return e.string
ensure
$stderr = STDERR
end
def test_loaderror
e = assert_raise(LoadError) do
require ‘nonexisting’
end
assert_match(/not find RubyGem nonexisting/, e.message)
end
def test_1_old_require_normal
ok = require ‘observer’ # is shipped with Ruby
assert_equal(true, ok) # first time we load it
ok = require ‘observer’
assert_equal(false, ok) # second time… its already loaded
end
def test_2_old_require_version
warning = capture_stderr do
require ‘observer’, ‘> 6.6.6’ # shipped with Ruby, but has no version
end
assert_match(/version number has therefore been ignored/, warning)
end
def test_1_gem_require_failed
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
def test_2_gem_require_version
e = assert_raise(LoadError) do
require ‘progressbar’, ‘>= 0.0.4’ # 0.0.3 is shipped with rubygems
end
assert_match(/version error: progressbar(0.0.3 not >= 0.0.4)/, e.message)
end
def test_3_gem_require_normal
ok = require ‘progressbar’ # is shipped with rubygems
assert_equal(true, ok) # first time we load it
ok = require ‘progressbar’
assert_equal(false, ok) # second time… its already loaded
end
end
if $0 == FILE
require ‘test/unit/ui/console/testrunner’
Test::Unit::UI::Console::TestRunner.run(TestGemRequireHack)
end
When running the testsuite it should output:
ruby test_gemrequirehack.rb
Loaded suite TestGemRequireHack
Started
…
Finished in 0.085952 seconds.
6 tests, 12 assertions, 0 failures, 0 errors
–
Simon Strandgaard