# A 'require' question

**URL:** <https://www.ruby-forum.com/t/a-require-question/81279>\
**Category:** Ruby\
**Created:** [January 6, 2007, 7:31pm UTC](https://www.ruby-forum.com/t/a-require-question/81279 "2007-01-06T19:31:00Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![bbiker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/97f17d/32.png) [@bbiker](https://www.ruby-forum.com/u/bbiker)\
**Post date:** [January 6, 2007, 7:31pm UTC](https://www.ruby-forum.com/t/a-require-question/81279/1 "2007-01-06T19:31:00Z")

</div>

When you have multiple versions of a gem, “require ‘gemxxx’” loads in  
the latest version.

How can you specify a specific version (or some other constraint)?

The ri documentation for require is of no help.

* * *

## Kernel#require require(string) =\> true or false

Ruby tries to load the library named _string_, returning +true+ if  
successful. If the filename does not resolve to an absolute path,  
it will be searched for in the directories listed in +$:+. If the  
file has the extension `.rb'', it is loaded as a source file; if the extension is`.so’’, `.o'', or`.dll’’, or whatever the  
default shared library extension is on the current platform, Ruby  
loads the shared library as a Ruby extension. Otherwise, Ruby tries  
adding `.rb'',`.so’’, and so on to the name. The name of the  
loaded feature is added to the array in +$"+. A feature will not be  
loaded if it’s name already appears in +$"+. However, the file name  
is not converted to an absolute path, so that ``+require  
‘a’;require ‘./a’+’’ will load +a.rb+ twice.

```
    require "my-library.rb"
    require "db-driver"
```

---

<div class="post-metadata">

**Author:** ![bbiker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/97f17d/32.png) [@bbiker](https://www.ruby-forum.com/u/bbiker)\
**Post date:** [January 19, 2007, 4:30pm UTC](https://www.ruby-forum.com/t/a-require-question/81279/2 "2007-01-19T16:30:00Z")

</div>

require\_gem ‘gem\_name’, ‘\>=1.2.3’

Though newest version of gems now has just

gem ‘gem\_name’, ‘\>=1.2.3’

Jason

---

<div class="post-metadata">

**Author:** ![bbiker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/97f17d/32.png) [@bbiker](https://www.ruby-forum.com/u/bbiker)\
**Post date:** [January 19, 2007, 4:30pm UTC](https://www.ruby-forum.com/t/a-require-question/81279/3 "2007-01-19T16:30:21Z")

</div>

On Jan 5, 2007, at 09:45, bbiker wrote:

> When you have multiple versions of a gem, “require ‘gemxxx’” loads in  
> the latest version.
> 
> How can you specify a specific version (or some other constraint)?

gem ‘gemname’, ‘= 1.2.3’ # \<, \<=, =, \>= and \> are all understood  
require ‘the\_file\_you\_want’

> The ri documentation for require is of no help.

RubyGems will build and install RDoc and ri for itself in the next  
release.

–  
Eric H. - removed\_email\_address@domain.invalid - [http://blog.segment7.net](http://blog.segment7.net)

I LIT YOUR GEM ON FIRE!

---

<div class="post-metadata">

**Author:** ![bbiker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/97f17d/32.png) [@bbiker](https://www.ruby-forum.com/u/bbiker)\
**Post date:** [September 25, 2007, 10:32pm UTC](https://www.ruby-forum.com/t/a-require-question/81279/4 "2007-09-25T22:32:46Z")

</div>

On Jan 5, 2007, at 10:57, Jason R. wrote:

> On 1/5/07, bbiker [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > When you have multiple versions of a gem, “require ‘gemxxx’” loads in  
> > the latest version.
> > 
> > How can you specify a specific version (or some other constraint)?
> 
> require\_gem ‘gem\_name’, ‘\>=1.2.3’

require\_gem was deprecated in 0.9.0. Switch to ‘gem’, don’t  
recommend its use.

> Though newest version of gems now has just
> 
> gem ‘gem\_name’, ‘\>=1.2.3’

–  
Eric H. - removed\_email\_address@domain.invalid - [http://blog.segment7.net](http://blog.segment7.net)

A: Yes  
Q: Is top-posting bad?  
— Derek Milhous Zumsteg

---

<div class="post-metadata">

**Author:** ![bbiker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/97f17d/32.png) [@bbiker](https://www.ruby-forum.com/u/bbiker)\
**Post date:** [September 25, 2007, 10:40pm UTC](https://www.ruby-forum.com/t/a-require-question/81279/5 "2007-09-25T22:40:38Z")

</div>

On 1/5/07, bbiker [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I meant how do you specify a version or other constraints with  
> “require”
> 
> for example:  
> require ‘gem\_name’
> 
> require can only take one parameter
> 
> thus require ‘gem\_name’, ‘\>=1.23’ responds with:

you need three commands:

1. require ‘rubygems’ # may be omitted when RUBYOPT=-rubygems or ruby  
is started with -rubygems

2. gem ‘gem\_name’ , ‘\>=1.23’ # this sets the required version and this  
is what eric wrote about

3. require ‘gem\_name’ # to actually require the file - no version or  
anything

Actually the gem\_name in he steps 2. and 3. may be different - one is  
the name of the gem (win32-shortcut for example) while the latter is  
the name of the file to be required (win32/shortcut)

If and only if you’ll see undefined method ‘gem’ in the second step,  
then replace ‘gem’ with ‘require\_gem’ (meaning you have an old version  
of rubygems, and you might want to upgrade)

---

<div class="post-metadata">

**Author:** ![bbiker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/97f17d/32.png) [@bbiker](https://www.ruby-forum.com/u/bbiker)\
**Post date:** [September 25, 2007, 10:48pm UTC](https://www.ruby-forum.com/t/a-require-question/81279/6 "2007-09-25T22:48:35Z")

</div>

On Jan 5, 2007, at 13:45, bbiker wrote:

> Eric H. wrote:
> 
> > On Jan 5, 2007, at 10:57, Jason R. wrote:
> > 
> > > Though newest version of gems now has just
> > > 
> > > gem ‘gem\_name’, ‘\>=1.2.3’
> 
> Sorry, I think I posed my question badly.
> 
> I meant how do you specify a version or other constraints with  
> “require”

You can’t. You use #gem instead of #require.

> LoadError: no such file to load – gem\_name \>=1.23
> 
> yes, I did use a gem name that I had on my machine

No, you didn’t. There is no ‘zentest’ gem.

> I tried in irb:  
> gem ‘zentest’, ‘\>=3.4.3’, response was  
> Gem::LoadError: Could not find RubyGem zentest \<\>=3.4.3
> 
> gem ‘zentest’ response was  
> Gem::LoadError: Could not find RubyGem zentest \<\>=0.0.0

Since RubyGems couldn’t find a ‘zentest’ gem, you must not have it  
installed.

There is a ‘ZenTest’ gem, though:

$ gem list -r zentest

\*\*\* REMOTE GEMS \*\*\*  
Bulk updating Gem source index for: [http://gems.rubyforge.org](http://gems.rubyforge.org)

ZenTest (3.4.3, 3.4.2, 3.4.1, 3.4.0, 3.3.0, 3.2.0, 3.1.0, 3.0.0)  
ZenTest provides 4 different tools and 1 library: zentest,  
unit\_diff, autotest, multiruby, and Test::Rails.  
^^^^^^^

capitalization matters.

–  
Eric H. - removed\_email\_address@domain.invalid - [http://blog.segment7.net](http://blog.segment7.net)

I LIT YOUR GEM ON FIRE!

---

<div class="post-metadata">

**Author:** ![bbiker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/97f17d/32.png) [@bbiker](https://www.ruby-forum.com/u/bbiker)\
**Post date:** [September 25, 2007, 10:49pm UTC](https://www.ruby-forum.com/t/a-require-question/81279/7 "2007-09-25T22:49:32Z")

</div>

Eric H. wrote:

> On Jan 5, 2007, at 10:57, Jason R. wrote:

> > Though newest version of gems now has just
> > 
> > gem ‘gem\_name’, ‘\>=1.2.3’

Sorry, I think I posed my question badly.

I meant how do you specify a version or other constraints with  
“require”

for example:  
require ‘gem\_name’

require can only take one parameter

thus require ‘gem\_name’, ‘\>=1.23’ responds with:

ArgumentError: wrong number of arguments (2 for 1)

and require ‘gem\_name \>=1.23’ responds with

LoadError: no such file to load – gem\_name \>=1.23

yes, I did use a gem name that I had on my machine

I tried in irb:  
gem ‘zentest’, ‘\>=3.4.3’, response was  
Gem::LoadError: Could not find RubyGem zentest \<\>=3.4.3

gem ‘zentest’ response was  
Gem::LoadError: Could not find RubyGem zentest \<\>=0.0.0

I have gem 0.9.0 and ruby 1.8.5 on Windows

---

<div class="post-metadata">

**Author:** ![bbiker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/97f17d/32.png) [@bbiker](https://www.ruby-forum.com/u/bbiker)\
**Post date:** [September 25, 2007, 10:50pm UTC](https://www.ruby-forum.com/t/a-require-question/81279/8 "2007-09-25T22:50:40Z")

</div>

Jan S. wrote:

> Actually the gem\_name in he steps 2. and 3. may be different - one is  
> the name of the gem (win32-shortcut for example) while the latter is  
> the name of the file to be required (win32/shortcut)
> 
> If and only if you’ll see undefined method ‘gem’ in the second step,  
> then replace ‘gem’ with ‘require\_gem’ (meaning you have an old version  
> of rubygems, and you might want to upgrade)

Thank you all for your responses. I now have a clearer understanding of  
‘require’ and ‘gem’ and how they cooperate.
