[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Getting the version number for rubygems?

Gregory Brown

1/21/2007 4:12:00 AM

Hi,

In ruport I have a tool that generates a bunch of boilerplate code for
folks, and in it, I use require_gem to lock the files down to a
specific version of Ruport.

I'd like to make this friendly to RubyGems 0.9.1 by using the gem
method rather than require_gem when people have 0.9.1, but want to
generate require_gem for older versions.

So basically, is there a constant or method I can call that'll give me
back the version of rubygems that i'm running?

21 Answers

Grauenwolf@gmail.com

1/21/2007 4:39:00 AM

0

Well, from the command line you can call "gem -v" to get the version.
Perhaps there is something you can do with that.

Jonathan

--
Posted via http://www.ruby-....

Devin Mullins

1/21/2007 4:41:00 AM

0

Gregory Brown wrote:
> I'd like to make this friendly to RubyGems 0.9.1 by using the gem
> method rather than require_gem when people have 0.9.1, but want to
> generate require_gem for older versions.
>
> So basically, is there a constant or method I can call that'll give me
> back the version of rubygems that i'm running?

For 0.8.11 and 0.9.0, this should work:
require 'rubygems/rubygems_version'
p Gem::RubyGemsVersion
but no promises on any other version. Check the SCM history.

That said, why not rescue NoMethodError?

Devin

Eric Hodel

1/21/2007 4:42:00 AM

0

On Jan 20, 2007, at 20:11, Gregory Brown wrote:
> In ruport I have a tool that generates a bunch of boilerplate code for
> folks, and in it, I use require_gem to lock the files down to a
> specific version of Ruport.
>
> I'd like to make this friendly to RubyGems 0.9.1 by using the gem
> method rather than require_gem when people have 0.9.1, but want to
> generate require_gem for older versions.

Just use #gem, and don't bother being backwards-compatible. RubyGems
older than 0.9.1 has a serious security exploit. (About 20% of
rubyists are running a version of RubyGems without #gem (prior to
0.9.1's release).)

> So basically, is there a constant or method I can call that'll give me
> back the version of rubygems that i'm running?

Don't think so hard:

require 'rubygems'
Kernel.respond_to? :gem

And now for the real answer to your question:

The rubygems constant is in Gem::RubyGemsVersion, which you can get
from 'rubygems/rubygems_version'. Kernel#gem first appeared in 0.9.0.

--
Eric Hodel - drbrain@segment7.net - http://blog.se...

I LIT YOUR GEM ON FIRE!


Gregory Brown

1/21/2007 4:50:00 AM

0

On 1/20/07, Eric Hodel <drbrain@segment7.net> wrote:
> On Jan 20, 2007, at 20:11, Gregory Brown wrote:
> > In ruport I have a tool that generates a bunch of boilerplate code for
> > folks, and in it, I use require_gem to lock the files down to a
> > specific version of Ruport.
> >
> > I'd like to make this friendly to RubyGems 0.9.1 by using the gem
> > method rather than require_gem when people have 0.9.1, but want to
> > generate require_gem for older versions.
>
> Just use #gem, and don't bother being backwards-compatible. RubyGems
> older than 0.9.1 has a serious security exploit. (About 20% of
> rubyists are running a version of RubyGems without #gem (prior to
> 0.9.1's release).)

I'll probably do this by the next release, but the one coming up in a
week or two i'd like to give people a bit of grace period.

> > So basically, is there a constant or method I can call that'll give me
> > back the version of rubygems that i'm running?
>
> Don't think so hard:
>
> require 'rubygems'
> Kernel.respond_to? :gem

Yep, that's better. thanks.

Jeremy McAnally

1/21/2007 5:04:00 AM

0

require 'rubygems'

unless Kernel.respond_to? :gem
alias gem require_gem
end

gem 'ruport'

That should work I believe...

--Jeremy

On 1/20/07, Gregory Brown <gregory.t.brown@gmail.com> wrote:
> On 1/20/07, Eric Hodel <drbrain@segment7.net> wrote:
> > On Jan 20, 2007, at 20:11, Gregory Brown wrote:
> > > In ruport I have a tool that generates a bunch of boilerplate code for
> > > folks, and in it, I use require_gem to lock the files down to a
> > > specific version of Ruport.
> > >
> > > I'd like to make this friendly to RubyGems 0.9.1 by using the gem
> > > method rather than require_gem when people have 0.9.1, but want to
> > > generate require_gem for older versions.
> >
> > Just use #gem, and don't bother being backwards-compatible. RubyGems
> > older than 0.9.1 has a serious security exploit. (About 20% of
> > rubyists are running a version of RubyGems without #gem (prior to
> > 0.9.1's release).)
>
> I'll probably do this by the next release, but the one coming up in a
> week or two i'd like to give people a bit of grace period.
>
> > > So basically, is there a constant or method I can call that'll give me
> > > back the version of rubygems that i'm running?
> >
> > Don't think so hard:
> >
> > require 'rubygems'
> > Kernel.respond_to? :gem
>
> Yep, that's better. thanks.
>
>


--
My free Ruby e-book:
http://www.humblelittlerubybook...

My blogs:
http://www.mrneigh...
http://www.rubyinpra...

Gregory Brown

1/21/2007 5:06:00 AM

0

On 1/21/07, Jeremy McAnally <jeremymcanally@gmail.com> wrote:
> require 'rubygems'
>
> unless Kernel.respond_to? :gem
> alias gem require_gem
> end
>
> gem 'ruport'
>
> That should work I believe...

instead of alias i'll use alias_method. But I bet that would work.

Devin Mullins

1/21/2007 5:11:00 AM

0

Gregory Brown wrote:
> instead of alias i'll use alias_method.
Why? (Curious, not argumentative.)

Devin

Gregory Brown

1/21/2007 5:30:00 AM

0

On 1/21/07, Devin Mullins <twifkak@comcast.net> wrote:
> Gregory Brown wrote:
> > instead of alias i'll use alias_method.
> Why? (Curious, not argumentative.)

alias is really a somewhat scary function. I can't think of a good
example right now, but i've seen a couple different surprising things
with alias, wheras alias_method is simple and has normal behaviour.
Perhaps someone on the list can help me out with this.

Gregory Brown

1/21/2007 5:30:00 AM

0

On 1/21/07, Gregory Brown <gregory.t.brown@gmail.com> wrote:
> On 1/21/07, Devin Mullins <twifkak@comcast.net> wrote:
> > Gregory Brown wrote:
> > > instead of alias i'll use alias_method.
> > Why? (Curious, not argumentative.)
>
> alias is really a somewhat scary function.

well, keyword. alias_method is actually a method, alias is not.

Eric Hodel

1/21/2007 6:40:00 AM

0

On Jan 20, 2007, at 21:03, Jeremy McAnally wrote:
> require 'rubygems'
>
> unless Kernel.respond_to? :gem
> alias gem require_gem
> end
>
> gem 'ruport'
>
> That should work I believe...

NO! NO! NO! NO! NO! NO! NO! NO! NO! NO! NO! NO! NO!

#gem and #require_gem do different things (autorequire).

You'll screw over other gems by aliasing things around.

Instead, just fall back to require_gem when gem isn't loaded.

if Kernel.respond_to? :gem then
gem blah
else
require_gem blah
end

DO NOT go aliasing methods on top of each other. #require and
#require_gem do different things, much like #sub and #sub! do
different things, respect that.

--
Eric Hodel - drbrain@segment7.net - http://blog.se...

I LIT YOUR GEM ON FIRE!