Current rubygems require idiom

B

Bil Kleb

Hi,

I currently have,

begin
require 'funit'
rescue LoadError
require 'rubygems'
require 'funit'
end

What /should/ I be using?

Thanks,
 
T

Trans

Hi,

I currently have,

begin
require 'funit'
rescue LoadError
require 'rubygems'
require 'funit'
end

What /should/ I be using?

Set the evironment var:

$ export RUBYOPT=rubygems

then require as normal:

require 'funit'

T.
 
B

Bil Kleb

Trans said:
Set the evironment var:

$ export RUBYOPT=rubygems


This is for an executable packaged in a gem, i.e.,
I have no control over the user's RUBYOPT variable?

(Note: the gem is also available as a tarball,
hence the initial attempt w/o rubygems.)

Later,
 
D

Daniel Berger

Bil said:
Hi,

I currently have,

begin
require 'funit'
rescue LoadError
require 'rubygems'
require 'funit'
end

What /should/ I be using?

In environments where I have multiple Ruby installations, where some use
rubygems and some don't, I do this:

require 'rubygems' rescue nil
require 'funit'

Regards,

Dan
 
D

Daniel DeLorme

Daniel said:
In environments where I have multiple Ruby installations, where some use
rubygems and some don't, I do this:

require 'rubygems' rescue nil
require 'funit'

Er, that can't possibly work since LoadError isn't a subclass of
StandardError
LoadError: no such file to load -- randomfile

Daniel
 
B

Ben Bleything

begin
require 'funit'
rescue LoadError
require 'rubygems'
require 'funit'
end

What /should/ I be using?

We do something like this:

begin
require 'something'
require 'anotherthing'
rescue LoadError
unless const_defined?( :Gem )
require 'rubygems'
retry
end
end

That may not be exactly it, I'll check when I get to work and re-post if
I left something out.

Ben
 
A

Austin Ziegler

This is for an executable packaged in a gem, i.e.,
I have no control over the user's RUBYOPT variable?

(Note: the gem is also available as a tarball,
hence the initial attempt w/o rubygems.)

If that's the case, don't bother doing anything special. Just require
your own code. Let's say you've got foo-1.0 with bin/foo that requires
lib/foo/foo.rb.

If installed as a tarball, bin/foo is installed into your standard bin
directory and it will just require 'foo/foo' as normal, because
lib/foo/foo.rb will be in SITE_RUBY.

if installed as a gem, bin/foo is installed into the gem's directory,
and an executable stub is installed into the standard bin directory.
The executable stub will do something like "gem 'foo'", and then load
"$(PATH_TO_FOO_GEM)/bin/foo".

Either way, you'll get the right behaviour.

-austin
 
T

Tim Pease

Yeah, yeah, it should have been "rescue LoadError nil".

I've found this little snippet to be useful for loading other people's gems ...

begin
require 'funit'
rescue LoadError
require 'rubygems'
raise unless gem 'funit'
retry
end

When I want to load code from my own gem ...

begin
require 'my_stuff'
rescue LoadError
path = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
raise if $:.include? path
$: << path
retry
end

I would put something like that in an executable Ruby script found in
the bin/ directory of my package. It also works for test files, etc.

Blessings,
TwP



Blessings,
TwP
 
S

Stefan Rusterholz

Ben said:
We do something like this:

begin
require 'something'
require 'anotherthing'
rescue LoadError
unless const_defined?( :Gem )
require 'rubygems'
retry
end
end

That may not be exactly it, I'll check when I get to work and re-post if
I left something out.

Ben

Why not just a corrected version of an earlier statement?
begin require 'rubygems' rescue LoadError; end
require 'allofyourstuff'

Seems cleaner to me

Regards
Stefan
 
B

Ben Bleything

Why not just a corrected version of an earlier statement?
begin require 'rubygems' rescue LoadError; end
require 'allofyourstuff'

Two reasons: first, my option only requires rubygems if it is necessary
to load the libraries. This is valuable because of the second reason,
namely that in our environment many libraries are installed in the
"traditional" way on our servers, so there's no need to require
rubygems.

In my personal projects, I use the rescue version. I was just giving
Bil another option.

Ben
 
A

ara.t.howard

Hi,

I currently have,

begin
require 'funit'
rescue LoadError
require 'rubygems'
require 'funit'
end

What /should/ I be using?

Thanks,

i personally use

begin
require 'rubygems'
rescue LoadError
42
end

require 'a'
require 'b'
require 'c'
require 'd'

as it's quite a bit drier and easier to comment out the rubygems when
you want to test a 'normal' or otherwise non-rubygems install.

kind regards.

a @ http://drawohara.com/
 
E

Eric Hodel

i personally use

begin
require 'rubygems'
rescue LoadError
42
end

require 'a'
require 'b'
require 'c'
require 'd'

as it's quite a bit drier and easier to comment out the rubygems
when you want to test a 'normal' or otherwise non-rubygems install.

No need to comment anything out, just use ruby -I to override
RubyGems. So long as the files you require are already in $LOAD_PATH
RubyGems won't activate any gems.
 
E

Eric Hodel

I currently have,

begin
require 'funit'
rescue LoadError
require 'rubygems'
require 'funit'
end

What /should/ I be using?

This is correct and will work.

As a RubyGems maintainer I recommend:

begin
require 'rubygems'
rescue LoadError
end

require 'funit'
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top