Forum: Ruby Getting Gem Version from Within a Gem

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.
Frew S. (Guest)
on 2007-07-10 10:40
Does anyone know if there is a way that I can have my program output
what version it is and get the version from the Gem that it is installed
from?

Thanks!
-fREW
Kuba (Guest)
on 2007-07-10 11:34
(Received via mailing list)
On 7/10/07, Frew S. <removed_email_address@domain.invalid> wrote:
> Does anyone know if there is a way that I can have my program output
> what version it is and get the version from the Gem that it is installed
> from?

Using 'gem' , you can execute sth. like:

 gem list --local | grep <your_prog_name>

but i'm not sure, is it what you expect.

Best Regards, Kuba.
Frew S. (Guest)
on 2007-07-10 17:37
Kuba wrote:
> On 7/10/07, Frew S. <removed_email_address@domain.invalid> wrote:
>> Does anyone know if there is a way that I can have my program output
>> what version it is and get the version from the Gem that it is installed
>> from?
>
> Using 'gem' , you can execute sth. like:
>
>  gem list --local | grep <your_prog_name>
>
> but i'm not sure, is it what you expect.
>
> Best Regards, Kuba.

So there's not a variable inside the program that I can access that
contains the Gem info?
Wayne E. Seguin (Guest)
on 2007-07-10 18:57
(Received via mailing list)
On Jul 10, 2007, at 09:37 , Frew S. wrote:
> So there's not a variable inside the program that I can access that
> contains the Gem info?

Frew,

The usual method of accessing the version of a gem from within a
running Ruby app is:

GemConstName::Version::STRING

This works if the author followed convention.
Frew S. (Guest)
on 2007-07-10 19:05
Wayne E. Seguin wrote:
> On Jul 10, 2007, at 09:37 , Frew S. wrote:
>> So there's not a variable inside the program that I can access that
>> contains the Gem info?
>
> Frew,
>
> The usual method of accessing the version of a gem from within a
> running Ruby app is:
>
> GemConstName::Version::STRING
>
> This works if the author followed convention.

I am the author of the application in question.  I tested your
suggestion by doing a

>> puts GemConstName::Version::STRING

to see if it would have the variable defined by the gem system as
defined in my gemspec, but it didn't work.  Is there something I need to
do to make this work?

Thanks for your help!

-fREW
Kuba P. (Guest)
on 2007-07-10 19:07
Frew S. wrote:
> Kuba wrote:
>> On 7/10/07, Frew S. <removed_email_address@domain.invalid> wrote:
>>> Does anyone know if there is a way that I can have my program output
>>> what version it is and get the version from the Gem that it is installed
>>> from?
>>
>> Using 'gem' , you can execute sth. like:
>>
>>  gem list --local | grep <your_prog_name>
>>
>> but i'm not sure, is it what you expect.
>>
>> Best Regards, Kuba.
>
> So there's not a variable inside the program that I can access that
> contains the Gem info?

I think, it depends on your gems' library/program.
There is no general variable (fix me if i'm wrong).

E.g.: In module 'Inline' is defined const. VERSION, so you can print
Inline::VERSION, but 'rake' defined const. RAKEVERSION
Frew S. (Guest)
on 2007-07-10 19:09
> E.g.: In module 'Inline' is defined const. VERSION, so you can print
> Inline::VERSION, but 'rake' defined const. RAKEVERSION

So what I really need to do here is define a constant in a separate file
as a module and include it in the gemspec and my other code.  Is that
correct?
Wayne E. Seguin (Guest)
on 2007-07-10 19:15
(Received via mailing list)
On Jul 10, 2007, at 11:05 , Frew S. wrote:
> Thanks for your help!
Frew, why yes there is!!! Thank you for clarifying the question. Tis
simple!

In lib/gem_name/version.rb place code like:
=================================
module GemName
   module Version
     # A method for comparing versions of required modules. It
expects two
     # arrays as parameters, and returns true if the first is no more
than the
     # second.
     def self.check(expected, actual) #:nodoc:
       good = false
       if actual[0] > expected[0]
         good = true
       elsif actual[0] == expected[0]
         if actual[1] > expected[1]
           good = true
         elsif actual[1] == expected[1] && actual[2] >= expected[2]
           good = true
         end
       end

       good
     end

     MAJOR = 0
     MINOR = 1
     TINY  = 2

     STRING = [MAJOR, MINOR, TINY].join(".")

     SSH_REQUIRED = [1,0,10]
     SFTP_REQUIRED = [1,1,0]
   end
end
=================================

Then require the version file in one of the main entry point files:

require "lib/gem_name/version"

Hope this helps! Gems are so much fun to develop.
Wayne E. Seguin (Guest)
on 2007-07-10 19:20
(Received via mailing list)
On Jul 10, 2007, at 11:13 , Wayne E. Seguin wrote:
> <snip>
>     SSH_REQUIRED = [1,0,10]
>     SFTP_REQUIRED = [1,1,0]
> <snip>

Sorry, these aren't required, they were specific to my gem.
Frew S. (Guest)
on 2007-07-10 19:24
Wayne E. Seguin wrote:
> On Jul 10, 2007, at 11:05 , Frew S. wrote:
>> Thanks for your help!
> Frew, why yes there is!!! Thank you for clarifying the question. Tis
> simple!
>
> In lib/gem_name/version.rb place code like:
> =================================
> module GemName
>    module Version
>      # A method for comparing versions of required modules. It
> expects two
>      # arrays as parameters, and returns true if the first is no more
> than the
>      # second.
>      def self.check(expected, actual) #:nodoc:
>        good = false
>        if actual[0] > expected[0]
>          good = true
>        elsif actual[0] == expected[0]
>          if actual[1] > expected[1]
>            good = true
>          elsif actual[1] == expected[1] && actual[2] >= expected[2]
>            good = true
>          end
>        end
>
>        good
>      end
>
>      MAJOR = 0
>      MINOR = 1
>      TINY  = 2
>
>      STRING = [MAJOR, MINOR, TINY].join(".")
>
>      SSH_REQUIRED = [1,0,10]
>      SFTP_REQUIRED = [1,1,0]
>    end
> end
> =================================
>
> Then require the version file in one of the main entry point files:
>
> require "lib/gem_name/version"
>
> Hope this helps! Gems are so much fun to develop.

Ok, I want to ensure that I am doing this the correct way, so is this
right?

lib/delish/version.rb
=====================
module Delish

  module Version
    MAJOR = 0
    MINOR = 7
    TINY  = 9

    STRING = [MAJOR, MINOR, TINY].join(".")
  end

end
=====================
Wayne E. Seguin (Guest)
on 2007-07-10 19:33
(Received via mailing list)
On Jul 10, 2007, at 11:24 , Frew S. wrote:
>     MINOR = 7
>     TINY  = 9
>
>     STRING = [MAJOR, MINOR, TINY].join(".")
>   end
>
> end
> =====================

Looks correct to me. You still might want to provide the check method
as it provides an interface for versioning checks.

So now you should be able to report the version from within your app
via:
Delish::Version::STRING
Frew S. (Guest)
on 2007-07-10 19:37
> Looks correct to me. You still might want to provide the check method
> as it provides an interface for versioning checks.
>
> So now you should be able to report the version from within your app
> via:
> Delish::Version::STRING

So if I did end up using the check method would I do something like this


Delish::Version::check(GTK::Version, Delish::Version::GTK_VERSION)
# Some gtk code here

Or something like that?

What's the use of this if the gemspec already has dependencies like
this?

Thanks again for your help!

-fREW
Eric H. (Guest)
on 2007-07-10 19:54
(Received via mailing list)
On Jul 10, 2007, at 08:24, Frew S. wrote:
>     TINY  = 9
>
>     STRING = [MAJOR, MINOR, TINY].join(".")
>   end
>
> end

UGH!

KISS, or employ YAGNI.

module Delish

   VERSION = '0.7.9'

end

You don't need major/minor/tiny.
Eric H. (Guest)
on 2007-07-10 19:55
(Received via mailing list)
On Jul 9, 2007, at 23:40, Frew S. wrote:

> Does anyone know if there is a way that I can have my program output
> what version it is and get the version from the Gem that it is
> installed
> from?

Gem.loaded_specs.map do |n,s| s.full_name end

Better to just have a VERSION constant somewhere, though.
Ryan D. (Guest)
on 2007-07-11 05:02
(Received via mailing list)
On Jul 9, 2007, at 23:40 , Frew S. wrote:

> Does anyone know if there is a way that I can have my program output
> what version it is and get the version from the Gem that it is
> installed
> from?

invert!

Have your program know what its version is and have your gemspec ask it:

=== lib/blah.rb

class Blah
   VERSION = "1.2.3"
   # ...
end

=== Rakefile

require 'rubygems'
require 'hoe'

require './lib/blah.rb'

Hoe.new("Blah", Blah::VERSION) do |p| # this builds the gemspec and
defines a lot of commands
   # ...
end
Wayne E. Seguin (Guest)
on 2007-07-12 01:12
(Received via mailing list)
On Jul 10, 2007, at 21:01 , Ryan D. wrote:
> require 'rubygems'
> require 'hoe'
>
> require './lib/blah.rb'
>
> Hoe.new("Blah", Blah::VERSION) do |p| # this builds the gemspec and
> defines a lot of commands
>   # ...
> end

Frew,

If you don't mind requiring Hoe then this is an excellent approach to
use.
Ryan D. (Guest)
on 2007-07-13 11:34
(Received via mailing list)
On Jul 11, 2007, at 14:11 , Wayne E. Seguin wrote:

>> require './lib/blah.rb'
>>
>> Hoe.new("Blah", Blah::VERSION) do |p| # this builds the gemspec
>> and defines a lot of commands
>>   # ...
>> end
>
> If you don't mind requiring Hoe then this is an excellent approach
> to use.

Huh? Even without hoe the point and the example still stand.
This topic is locked and can not be replied to.