Current rubygems require idiom

Hi,

I currently have,

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

What /should/ I be using?

Thanks,

···

--
Bil Kleb
http://nasarb.rubyforge.org

Set the evironment var:

  $ export RUBYOPT=rubygems

then require as normal:

  require 'funit'

T.

···

On Aug 20, 4:30 am, Bil Kleb <Bil.K...@NASA.gov> wrote:

Hi,

I currently have,

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

What /should/ I be using?

Bil Kleb wrote:

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

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 Mon, Aug 20, 2007, Bil Kleb wrote:

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

What /should/ I be using?

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 5:30 AM, Bil Kleb wrote:

Hi,

I currently have,

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

What /should/ I be using?

Thanks,
--
Bil Kleb
http://nasarb.rubyforge.org

--
we can deny everything, except that we have the possibility of being better. simply reflect on that.
h.h. the 14th dalai lama

This is correct and will work.

As a RubyGems maintainer I recommend:

begin
   require 'rubygems'
rescue LoadError
end

require 'funit'

···

On Aug 20, 2007, at 04:30, Bil Kleb wrote:

I currently have,

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

What /should/ I be using?

--
Poor workers blame their tools. Good workers build better tools. The
best workers get their tools to do the work for them. -- Syndicate Wars

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,

···

--
Bil Kleb
http://nasarb.rubyforge.org

Daniel Berger 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

Ben Bleything wrote:

···

On Mon, Aug 20, 2007, Bil Kleb 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
--
Posted via http://www.ruby-forum.com/\.

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.

···

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

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

Hi,

I currently have,

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

What /should/ I be using?

Thanks,
--
Bil Kleb
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.

--
Poor workers blame their tools. Good workers build better tools. The
best workers get their tools to do the work for them. -- Syndicate Wars

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

···

On 8/20/07, Bil Kleb <Bil.Kleb@nasa.gov> 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.)

--
Austin Ziegler * halostatue@gmail.com * http://www.halostatue.ca/
               * austin@halostatue.ca * You are in a maze of twisty little passages, all alike. // halo • statue
               * austin@zieglers.ca

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

Regards,

Dan

···

On Aug 20, 8:42 am, Daniel DeLorme <dan...@dan42.com> wrote:

Daniel Berger 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

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

···

On Tue, Aug 21, 2007, Stefan Rusterholz wrote:

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

Hi --

···

On Tue, 21 Aug 2007, Daniel Berger wrote:

On Aug 20, 8:42 am, Daniel DeLorme <dan...@dan42.com> wrote:

Daniel Berger 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".

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

David

--
* Books:
   RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242\)
   RUBY FOR RAILS (http://www.manning.com/black\)
* Ruby/Rails training
     & consulting: Ruby Power and Light, LLC (http://www.rubypal.com)

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 8/20/07, Daniel Berger <djberg96@gmail.com> wrote:

On Aug 20, 8:42 am, Daniel DeLorme <dan...@dan42.com> wrote:
> Daniel Berger 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".