A 'require' question

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"

require_gem ‘gem_name’, ‘>=1.2.3’

Though newest version of gems now has just

gem ‘gem_name’, ‘>=1.2.3’

Jason

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. - [email protected] - http://blog.segment7.net

I LIT YOUR GEM ON FIRE!

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

On 1/5/07, bbiker [email protected] 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. - [email protected] - http://blog.segment7.net

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

On 1/5/07, bbiker [email protected] 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)

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

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. - [email protected] - http://blog.segment7.net

I LIT YOUR GEM ON FIRE!

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

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.