--azLHFNyN32YCQGCU
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

On Sat, Nov 12, 2011 at 05:28:15AM +0900, 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?

For now, I'm going with the following.

lib/gem_name.rb:

    module GemName
      @version = Gem::Version.new 'N.N.N'
      def self.version; @version; end
      
      . . .
    end

gem_name.gemspec:

    load 'lib/gem_name.rb'
    
    Gem::Specification.new do |s|
      s.version = GemName.version.to_s
      . . .
    end

bin/gemname:

    #!/usr/bin/env ruby
    require 'gem_name'
    require 'optparse'
    
    . . .
    
    opts = OptionParser.new do |opts|
      . . .
      
      opts.on_tail('--version', help_text[:version]) do
        puts "#{0} #{GemName.version} . . ."
      end
    end

If anyone sees any problems with that approach, I'd like to hear about
it, but it seems to be working well enough for the moment.

-- 
Chad Perrin [ original content licensed OWL: http://owl.apotheon.org ]

--azLHFNyN32YCQGCU
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.14 (FreeBSD)

iEYEARECAAYFAk69m0gACgkQ9mn/Pj01uKWBywCguQt07q/Sp+1GILPFNoAmQflt
y1kAoKx44g0vPp8Wkm77VrdSf+PnhMZK
 ML
-----END PGP SIGNATURE-----

--azLHFNyN32YCQGCU--