Current rubygems require idiom

Hi,

I currently have,

begin
require ‘funit’
rescue LoadError
require ‘rubygems’
require ‘funit’
end

What /should/ I be using?

Thanks,

On Aug 20, 4:30 am, Bil K. [email protected] wrote:

What /should/ I be using?

Set the evironment var:

$ export RUBYOPT=rubygems

then require as normal:

require ‘funit’

T.

Bil K. wrote:

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

Daniel B. wrote:

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

require “randomfile” rescue nil
LoadError: no such file to load – randomfile

Daniel

On Mon, Aug 20, 2007, Bil K. wrote:

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

On Aug 20, 8:42 am, Daniel DeLorme [email protected] wrote:

Daniel B. wrote:

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

Yeah, yeah, it should have been “rescue LoadError nil”.

Regards,

Dan

Hi –

On Tue, 21 Aug 2007, Daniel B. wrote:

Er, that can’t possibly work since LoadError isn’t a subclass of
StandardError

Yeah, yeah, it should have been “rescue LoadError nil”.

I don’t think the error specification thing works with the one-line
rescue, though.

David

On 8/20/07, Daniel B. [email protected] wrote:

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

On Tue, Aug 21, 2007, Stefan R. wrote:

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

Ben B. wrote:

On Mon, Aug 20, 2007, Bil K. wrote:

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

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

Seems cleaner to me

Regards
Stefan

On Aug 20, 2007, at 5:30 AM, Bil K. wrote:

What /should/ I be using?

Thanks,

Bil K.
http://nasarb.rubyforge.org

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/

On Aug 20, 2007, at 10:20, ara.t.howard wrote:

end

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.

Trans wrote:

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,

On Aug 20, 2007, at 04:30, Bil K. wrote:

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’

On 8/20/07, Bil K. [email protected] wrote:

Trans wrote:

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.)

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs