Forum: Ruby Getting the version number for rubygems?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
31e038e4e9330f6c75ccfd1fca8010ee?d=identicon&s=25 Gregory Brown (Guest)
on 2007-01-21 05:12
(Received via mailing list)
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?
71c1027d2483fe242b0affc5e59df647?d=identicon&s=25 Jonathan Allen (grauenwolf)
on 2007-01-21 05:39
Well, from the command line you can call "gem -v" to get the version.
Perhaps there is something you can do with that.

Jonathan
918c6daad03c85e51ad1a11f57017947?d=identicon&s=25 Devin Mullins (twifkak)
on 2007-01-21 05:42
(Received via mailing list)
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
58479f76374a3ba3c69b9804163f39f4?d=identicon&s=25 Eric Hodel (Guest)
on 2007-01-21 05:43
(Received via mailing list)
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.segment7.net

I LIT YOUR GEM ON FIRE!
31e038e4e9330f6c75ccfd1fca8010ee?d=identicon&s=25 Gregory Brown (Guest)
on 2007-01-21 05:53
(Received via mailing list)
On 1/20/07, Eric Hodel <drbrain@segment7.net> wrote:
> 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.
6d3c187a8b3ef53b08e3e7e8572c4fea?d=identicon&s=25 Jeremy McAnally (Guest)
on 2007-01-21 06:04
(Received via mailing list)
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:
> > Just use #gem, and don't bother being backwards-compatible.  RubyGems
> > Don't think so hard:
> >
> > require 'rubygems'
> > Kernel.respond_to? :gem
>
> Yep, that's better. thanks.
>
>


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

My blogs:
http://www.mrneighborly.com/
http://www.rubyinpractice.com/
31e038e4e9330f6c75ccfd1fca8010ee?d=identicon&s=25 Gregory Brown (Guest)
on 2007-01-21 06:07
(Received via mailing list)
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.
918c6daad03c85e51ad1a11f57017947?d=identicon&s=25 Devin Mullins (twifkak)
on 2007-01-21 06:11
(Received via mailing list)
Gregory Brown wrote:
> instead of alias i'll use alias_method.
Why? (Curious, not argumentative.)

Devin
31e038e4e9330f6c75ccfd1fca8010ee?d=identicon&s=25 Gregory Brown (Guest)
on 2007-01-21 06:30
(Received via mailing list)
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.
31e038e4e9330f6c75ccfd1fca8010ee?d=identicon&s=25 Gregory Brown (Guest)
on 2007-01-21 06:31
(Received via mailing list)
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.
58479f76374a3ba3c69b9804163f39f4?d=identicon&s=25 Eric Hodel (Guest)
on 2007-01-21 07:40
(Received via mailing list)
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.segment7.net

I LIT YOUR GEM ON FIRE!
6d3c187a8b3ef53b08e3e7e8572c4fea?d=identicon&s=25 Jeremy McAnally (Guest)
on 2007-01-21 07:52
(Received via mailing list)
Maybe you should tell the Rails team because I'm fairly sure that's
how they planned on handling it...

http://dev.rubyonrails.org/ticket/6886

--Jeremy

On 1/21/07, Eric Hodel <drbrain@segment7.net> wrote:
>
> else
> I LIT YOUR GEM ON FIRE!
>
>
>


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

My blogs:
http://www.mrneighborly.com/
http://www.rubyinpractice.com/
31e038e4e9330f6c75ccfd1fca8010ee?d=identicon&s=25 Gregory Brown (Guest)
on 2007-01-21 08:10
(Received via mailing list)
On 1/21/07, Eric Hodel <drbrain@segment7.net> wrote:
>
> NO! NO! NO! NO! NO! NO! NO! NO! NO! NO! NO! NO! NO!

There you go being an asshole again Eric. (please stop)

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

I'm aware of this.  I actually need to just lock version and do a
normal require.

I'll do it that way.
58479f76374a3ba3c69b9804163f39f4?d=identicon&s=25 Eric Hodel (Guest)
on 2007-01-21 08:25
(Received via mailing list)
On Jan 20, 2007, at 21:29, Gregory Brown 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.  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.

You're seeing phantoms.  There's no difference between alias_method
and alias on the inside.

       case NODE_ALIAS:
         if (NIL_P(ruby_class)) {
             rb_raise(rb_eTypeError, "no class to make alias");
         }
         rb_alias(ruby_class, rb_to_id(rb_eval(self, node->u1.node)),
                              rb_to_id(rb_eval(self, node->u2.node)));

static VALUE
rb_mod_alias_method(mod, newname, oldname)
     VALUE mod, newname, oldname;
{
     rb_alias(mod, rb_to_id(newname), rb_to_id(oldname));


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

I LIT YOUR GEM ON FIRE!
31e038e4e9330f6c75ccfd1fca8010ee?d=identicon&s=25 Gregory Brown (Guest)
on 2007-01-21 08:34
(Received via mailing list)
On 1/21/07, Eric Hodel <drbrain@segment7.net> wrote:
>
> You're seeing phantoms.  There's no difference between alias_method
> and alias on the inside.

Hah.  That'd explain why I am scared.

I forget what the cases were, but Sebastian Delmont did a talk on
things he found surprising in Ruby, and the stuff he pulled up for
alias vs. alias_method was pretty convincing.
58479f76374a3ba3c69b9804163f39f4?d=identicon&s=25 Eric Hodel (Guest)
on 2007-01-21 08:42
(Received via mailing list)
On Jan 20, 2007, at 23:33, Gregory Brown wrote:
>> > with alias, wheras alias_method is simple and has normal behaviour.
>> > Perhaps someone on the list can help me out with this.
>>
>> You're seeing phantoms.  There's no difference between alias_method
>> and alias on the inside.
>
> Hah.  That'd explain why I am scared.
>
> I forget what the cases were, but Sebastian Delmont did a talk on
> things he found surprising in Ruby, and the stuff he pulled up for
> alias vs. alias_method was pretty convincing.

any slides online?

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

I LIT YOUR GEM ON FIRE!
31e038e4e9330f6c75ccfd1fca8010ee?d=identicon&s=25 Gregory Brown (Guest)
on 2007-01-21 08:44
(Received via mailing list)
On 1/21/07, Eric Hodel <drbrain@segment7.net> wrote:

> any slides online?

I didn't see them on the nycruby list. :-/
I'll email him and see if they're posted anywhere
31e038e4e9330f6c75ccfd1fca8010ee?d=identicon&s=25 Gregory Brown (Guest)
on 2007-01-21 18:48
(Received via mailing list)
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.)

I guess it really is just the method vs. keyword thing.
alias is available everywhere, which means you might be aliasing in
the wrong place

Also, alias can work on global variables and regex backreferences.

it's also scary to me to see:  alias new_method old_method
where alias_method :new_method, :old_method seems more natural to me.

I think I may have been overly fearful about the use of alias, but I
guess there is no good reason not to use it as long as you know what
you're doing.

regards,
-greg
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2007-01-21 21:07
(Received via mailing list)
On Jan 21, 2007, at 1:24 AM, Eric Hodel wrote:

>
> You're seeing phantoms.  There's no difference between alias_method
> and alias on the inside.

Well, you can override alias_method() if needed, but not alias.

James Edward Gray II
5a837592409354297424994e8d62f722?d=identicon&s=25 Ryan Davis (Guest)
on 2007-01-21 21:24
(Received via mailing list)
On Jan 21, 2007, at 12:07 PM, James Edward Gray II wrote:

> On Jan 21, 2007, at 1:24 AM, Eric Hodel wrote:
>> You're seeing phantoms.  There's no difference between
>> alias_method and alias on the inside.
> Well, you can override alias_method() if needed, but not alias.

And that changes or invalidates what Eric said... how? Not only did
he qualify with "on the inside" but he also backed it up by showing
said inside.
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2007-01-21 21:31
(Received via mailing list)
On Jan 21, 2007, at 2:24 PM, Ryan Davis wrote:

> said inside.
Eric showed the implementation of a method.  Methods are
overridable.  alias is implemented as a keyword, internally.
Keywords are not overridable.

James Edward Gray II
31e038e4e9330f6c75ccfd1fca8010ee?d=identicon&s=25 Gregory Brown (Guest)
on 2007-01-21 21:40
(Received via mailing list)
On 1/21/07, James Edward Gray II <james@grayproductions.net> wrote:

> Eric showed the implementation of a method.  Methods are
> overridable.  alias is implemented as a keyword, internally.
> Keywords are not overridable.

Also the interface and the behaviours are different, as I mentioned in
an earlier post.  It's true I was being paranoid by saying it is
'scary', but it is different on the surface, which to me is more
relevant
than the C sources.
This topic is locked and can not be replied to.