A 'require' question

B

bbiker

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"
 
E

Eric Hodel

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.

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

bbiker

Eric said:
On Jan 5, 2007, at 10:57, Jason Roelofs wrote:

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
 
J

Jan Svitok

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:

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

Eric Hodel

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

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

bbiker

Jan said:
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)

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

require problem 0
Extensions and 'require' 6
require problem 3
require - not loading gem 2
Basics of Require 5
Effects of require 3
"Load" Works, But "Require" Doesn't? 4
circular 'require' 28

Members online

Forum statistics

Threads
474,262
Messages
2,571,049
Members
48,769
Latest member
Clifft

Latest Threads

Top