# Current rubygems require idiom

**URL:** <https://rubytalk.org/t/current-rubygems-require-idiom/40053>\
**Category:** ruby-talk\
**Created:** [20 August 2007 11:30 UTC](https://rubytalk.org/t/current-rubygems-require-idiom/40053 "2007-08-20T11:30:03Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![BIl\_Kleb1](https://avatars.discourse-cdn.com/v4/letter/b/a587f6/32.png) [@BIl\_Kleb1](https://rubytalk.org/u/BIl_Kleb1)\
**Post date:** [20 August 2007 11:30 UTC](https://rubytalk.org/t/current-rubygems-require-idiom/40053/1 "2007-08-20T11:30:03Z")

</div>

Hi,

I currently have,

&nbsp;&nbsp;begin  
&nbsp;&nbsp;&nbsp;&nbsp;require 'funit'  
&nbsp;&nbsp;rescue LoadError  
&nbsp;&nbsp;&nbsp;&nbsp;require 'rubygems'  
&nbsp;&nbsp;&nbsp;&nbsp;require 'funit'  
&nbsp;&nbsp;end

What /should/ I be using?

Thanks,

> **···**
>
> --  
> Bil Kleb  
> [http://nasarb.rubyforge.org](http://nasarb.rubyforge.org)

---

<div class="post-metadata">

**Author:** ![7rans](https://avatars.discourse-cdn.com/v4/letter/7/a8b319/32.png) [@7rans](https://rubytalk.org/u/7rans)\
**Post date:** [20 August 2007 11:42 UTC](https://rubytalk.org/t/current-rubygems-require-idiom/40053/2 "2007-08-20T11:42:33Z")

</div>

Set the evironment var:

&nbsp;&nbsp;$ export RUBYOPT=rubygems

then require as normal:

&nbsp;&nbsp;require 'funit'

T.

> **···**
>
> On Aug 20, 4:30 am, Bil Kleb \<Bil.K...@NASA.gov\> wrote:
> 
> > Hi,
> > 
> > I currently have,
> > 
> > &nbsp;&nbsp;begin  
> > &nbsp;&nbsp;&nbsp;&nbsp;require 'funit'  
> > &nbsp;&nbsp;rescue LoadError  
> > &nbsp;&nbsp;&nbsp;&nbsp;require 'rubygems'  
> > &nbsp;&nbsp;&nbsp;&nbsp;require 'funit'  
> > &nbsp;&nbsp;end
> > 
> > What /should/ I be using?

---

<div class="post-metadata">

**Author:** ![Daniel\_Berger2](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/daniel_berger2/32/2243_2.png) [@Daniel\_Berger2](https://rubytalk.org/u/Daniel_Berger2)\
**Post date:** [20 August 2007 11:58 UTC](https://rubytalk.org/t/current-rubygems-require-idiom/40053/3 "2007-08-20T11:58:38Z")

</div>

Bil Kleb wrote:

> Hi,
> 
> I currently have,
> 
> begin  
> &nbsp;&nbsp;&nbsp;require 'funit'  
> rescue LoadError  
> &nbsp;&nbsp;&nbsp;require 'rubygems'  
> &nbsp;&nbsp;&nbsp;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

---

<div class="post-metadata">

**Author:** ![Ben\_Bleything1](https://avatars.discourse-cdn.com/v4/letter/b/3e96dc/32.png) [@Ben\_Bleything1](https://rubytalk.org/u/Ben_Bleything1)\
**Post date:** [20 August 2007 15:19 UTC](https://rubytalk.org/t/current-rubygems-require-idiom/40053/4 "2007-08-20T15:19:33Z")

</div>

We do something like this:

&nbsp;&nbsp;begin  
&nbsp;&nbsp;&nbsp;&nbsp;require 'something'  
&nbsp;&nbsp;&nbsp;&nbsp;require 'anotherthing'  
&nbsp;&nbsp;rescue LoadError  
&nbsp;&nbsp;&nbsp;&nbsp;unless const\_defined?( :Gem )  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;require 'rubygems'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;retry  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;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  
> > &nbsp;&nbsp;&nbsp;require 'funit'  
> > rescue LoadError  
> > &nbsp;&nbsp;&nbsp;require 'rubygems'  
> > &nbsp;&nbsp;&nbsp;require 'funit'  
> > end
> > 
> > What /should/ I be using?

---

<div class="post-metadata">

**Author:** ![a11](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/a11/32/8169_2.png) [@a11](https://rubytalk.org/u/a11)\
**Post date:** [20 August 2007 17:20 UTC](https://rubytalk.org/t/current-rubygems-require-idiom/40053/5 "2007-08-20T17:20:11Z")

</div>

i personally use

begin  
&nbsp;&nbsp;&nbsp;require 'rubygems'  
rescue LoadError  
&nbsp;&nbsp;&nbsp;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/](http://drawohara.com/)

> **···**
>
> On Aug 20, 2007, at 5:30 AM, Bil Kleb wrote:
> 
> > Hi,
> > 
> > I currently have,
> > 
> > begin  
> > &nbsp;&nbsp;&nbsp;require 'funit'  
> > rescue LoadError  
> > &nbsp;&nbsp;&nbsp;require 'rubygems'  
> > &nbsp;&nbsp;&nbsp;require 'funit'  
> > end
> > 
> > What /should/ I be using?
> > 
> > Thanks,  
> > --  
> > Bil Kleb  
> > [http://nasarb.rubyforge.org](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

---

<div class="post-metadata">

**Author:** ![Eric\_Hodel1](https://avatars.discourse-cdn.com/v4/letter/e/a9adbd/32.png) [@Eric\_Hodel1](https://rubytalk.org/u/Eric_Hodel1)\
**Post date:** [20 August 2007 20:47 UTC](https://rubytalk.org/t/current-rubygems-require-idiom/40053/6 "2007-08-20T20:47:10Z")

</div>

This is correct and will work.

As a RubyGems maintainer I recommend:

begin  
&nbsp;&nbsp;&nbsp;require 'rubygems'  
rescue LoadError  
end

require 'funit'

> **···**
>
> On Aug 20, 2007, at 04:30, Bil Kleb wrote:
> 
> > I currently have,
> > 
> > begin  
> > &nbsp;&nbsp;&nbsp;require 'funit'  
> > rescue LoadError  
> > &nbsp;&nbsp;&nbsp;require 'rubygems'  
> > &nbsp;&nbsp;&nbsp;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

---

<div class="post-metadata">

**Author:** ![BIl\_Kleb1](https://avatars.discourse-cdn.com/v4/letter/b/a587f6/32.png) [@BIl\_Kleb1](https://rubytalk.org/u/BIl_Kleb1)\
**Post date:** [20 August 2007 11:50 UTC](https://rubytalk.org/t/current-rubygems-require-idiom/40053/7 "2007-08-20T11:50:00Z")

</div>

Trans wrote:

> Set the evironment var:
> 
> &nbsp;&nbsp;$ 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](http://nasarb.rubyforge.org)

---

<div class="post-metadata">

**Author:** ![Daniel\_DeLorme](https://avatars.discourse-cdn.com/v4/letter/d/9d8465/32.png) [@Daniel\_DeLorme](https://rubytalk.org/u/Daniel_DeLorme)\
**Post date:** [20 August 2007 14:42 UTC](https://rubytalk.org/t/current-rubygems-require-idiom/40053/8 "2007-08-20T14:42:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Stefan\_Rusterholz](https://avatars.discourse-cdn.com/v4/letter/s/e495f1/32.png) [@Stefan\_Rusterholz](https://rubytalk.org/u/Stefan_Rusterholz)\
**Post date:** [20 August 2007 16:18 UTC](https://rubytalk.org/t/current-rubygems-require-idiom/40053/9 "2007-08-20T16:18:40Z")

</div>

Ben Bleything wrote:

> **···**
>
> > On Mon, Aug 20, 2007, Bil Kleb wrote:
> > 
> > > begin  
> > > &nbsp;&nbsp;&nbsp;require 'funit'  
> > > rescue LoadError  
> > > &nbsp;&nbsp;&nbsp;require 'rubygems'  
> > > &nbsp;&nbsp;&nbsp;require 'funit'  
> > > end
> > > 
> > > What /should/ I be using?
> > 
> > We do something like this:
> > 
> > &nbsp;&nbsp;begin  
> > &nbsp;&nbsp;&nbsp;&nbsp;require 'something'  
> > &nbsp;&nbsp;&nbsp;&nbsp;require 'anotherthing'  
> > &nbsp;&nbsp;rescue LoadError  
> > &nbsp;&nbsp;&nbsp;&nbsp;unless const\_defined?( :Gem )  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;require 'rubygems'  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;retry  
> > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;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/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Eric\_Hodel1](https://avatars.discourse-cdn.com/v4/letter/e/a9adbd/32.png) [@Eric\_Hodel1](https://rubytalk.org/u/Eric_Hodel1)\
**Post date:** [20 August 2007 20:42 UTC](https://rubytalk.org/t/current-rubygems-require-idiom/40053/10 "2007-08-20T20:42:30Z")

</div>

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  
> > > &nbsp;&nbsp;&nbsp;require 'funit'  
> > > rescue LoadError  
> > > &nbsp;&nbsp;&nbsp;require 'rubygems'  
> > > &nbsp;&nbsp;&nbsp;require 'funit'  
> > > end
> > > 
> > > What /should/ I be using?
> > > 
> > > Thanks,  
> > > --  
> > > Bil Kleb  
> > > [http://nasarb.rubyforge.org](http://nasarb.rubyforge.org)
> > 
> > i personally use
> > 
> > begin  
> > &nbsp;&nbsp;require 'rubygems'  
> > rescue LoadError  
> > &nbsp;&nbsp;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

---

<div class="post-metadata">

**Author:** ![Austin\_Ziegler5](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/austin_ziegler5/32/1985_2.png) [@Austin\_Ziegler5](https://rubytalk.org/u/Austin_Ziegler5)\
**Post date:** [20 August 2007 15:27 UTC](https://rubytalk.org/t/current-rubygems-require-idiom/40053/11 "2007-08-20T15:27:01Z")

</div>

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/](http://www.halostatue.ca/)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\* austin@halostatue.ca \* [You are in a maze of twisty little passages, all alike. // halo • statue](http://www.halostatue.ca/feed/)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\* austin@zieglers.ca

---

<div class="post-metadata">

**Author:** ![Daniel\_Berger2](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/daniel_berger2/32/2243_2.png) [@Daniel\_Berger2](https://rubytalk.org/u/Daniel_Berger2)\
**Post date:** [20 August 2007 15:35 UTC](https://rubytalk.org/t/current-rubygems-require-idiom/40053/12 "2007-08-20T15:35:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Ben\_Bleything1](https://avatars.discourse-cdn.com/v4/letter/b/3e96dc/32.png) [@Ben\_Bleything1](https://rubytalk.org/u/Ben_Bleything1)\
**Post date:** [20 August 2007 17:01 UTC](https://rubytalk.org/t/current-rubygems-require-idiom/40053/13 "2007-08-20T17:01:55Z")

</div>

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'

---

<div class="post-metadata">

**Author:** ![David\_A\_Black1](https://avatars.discourse-cdn.com/v4/letter/d/bcef8e/32.png) [@David\_A\_Black1](https://rubytalk.org/u/David_A_Black1)\
**Post date:** [20 August 2007 16:04 UTC](https://rubytalk.org/t/current-rubygems-require-idiom/40053/14 "2007-08-20T16:04:39Z")

</div>

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:  
> &nbsp;&nbsp;&nbsp;RAILS ROUTING (new! [http://www.awprofessional.com/title/0321509242\](http://www.awprofessional.com/title/0321509242%5C))  
> &nbsp;&nbsp;&nbsp;RUBY FOR RAILS ([http://www.manning.com/black\](http://www.manning.com/black%5C))  
> \* Ruby/Rails training  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;& consulting: Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))

---

<div class="post-metadata">

**Author:** ![Tim\_Pease](https://avatars.discourse-cdn.com/v4/letter/t/3e96dc/32.png) [@Tim\_Pease](https://rubytalk.org/u/Tim_Pease)\
**Post date:** [20 August 2007 16:05 UTC](https://rubytalk.org/t/current-rubygems-require-idiom/40053/15 "2007-08-20T16:05:04Z")

</div>

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

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

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

begin  
&nbsp;&nbsp;require 'my\_stuff'  
rescue LoadError  
&nbsp;&nbsp;path = File.expand\_path(File.join(File.dirname(\_\_FILE\_\_), '..', 'lib'))  
&nbsp;&nbsp;raise if $:.include? path  
&nbsp;&nbsp;$: \<\< path  
&nbsp;&nbsp;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".
