[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

A 'require' question

Bernard Kenik

1/5/2007 5:43:00 PM

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"

6 Answers

Eric Hodel

1/5/2007 7:00:00 PM

0

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 Hodel - drbrain@segment7.net - http://blog.se...

I LIT YOUR GEM ON FIRE!


Eric Hodel

1/5/2007 7:01:00 PM

0

On Jan 5, 2007, at 10:57, Jason Roelofs wrote:
> On 1/5/07, bbiker <renard@nc.rr.com> 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 Hodel - drbrain@segment7.net - http://blog.se...

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



Bernard Kenik

1/5/2007 9:42:00 PM

0


Eric Hodel wrote:
> On Jan 5, 2007, at 10:57, Jason Roelofs 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

Jano Svitok

1/5/2007 10:14:00 PM

0

On 1/5/07, bbiker <renard@nc.rr.com> wrote:
>
> Eric Hodel wrote:
> > On Jan 5, 2007, at 10:57, Jason Roelofs 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:

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)

Eric Hodel

1/6/2007 1:05:00 AM

0

On Jan 5, 2007, at 13:45, bbiker wrote:
> Eric Hodel wrote:
>> On Jan 5, 2007, at 10:57, Jason Roelofs 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.

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

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 Hodel - drbrain@segment7.net - http://blog.se...

I LIT YOUR GEM ON FIRE!


Bernard Kenik

1/6/2007 4:28:00 PM

0

Jan Svitok wrote:
>
> 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.