On 11/11/2011 14:28, Chad Perrin wrote:
> How do I specify and access a gem's version number within the code of the
> gem itself?  For example:
> 
> 1. I write a library.
> 2. I write a command line utility that uses that library.
> 3. The utility uses OptionParser, with a --version option.
> 4. I package the things up inside a gem.
> 
> What's the best way to specify the version number and give the code
> executed when the --version argument is supplied the ability to access
> that version number?

I don't know about the best way, but I've seen a couple ways to handle
it.  The easiest for your needs is to define a VERSION constant
somewhere in the namespace of the library that is set to a simple string
representing the version number.  I've seen other libraries break the
version up into pieces and set those to individual constants rather than
a single VERSION constant.

The trick with both of these schemes is to keep the internal version
information in sync with the gem version.  How you do that depends on
how you build your gem, but generally speaking, you have two fairly
decent options.

The first is to define your constant in a minimal version.rb file and
then load that to get the version when building your gem.  The other is
to generate the version.rb file based on some other specification of the
version when you build the gem.

Of course, you could also manually define your version in multiple
places.  That's crazy talk though as far as I'm concerned.

-Jeremy