From: Chad Perrin Date: 2011-11-12T07:04:15+09:00 Subject: Re: referring to version numbers in a gem --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: >=20 > 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. >=20 > 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 =3D Gem::Version.new 'N.N.N' def self.version; @version; end =20 . . . end gem_name.gemspec: load 'lib/gem_name.rb' =20 Gem::Specification.new do |s| s.version =3D GemName.version.to_s . . . end bin/gemname: #!/usr/bin/env ruby require 'gem_name' require 'optparse' =20 . . . =20 opts =3D OptionParser.new do |opts| . . . =20 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. --=20 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 =2GML -----END PGP SIGNATURE----- --azLHFNyN32YCQGCU--