Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the benefit of version.rb in Ruby gem

Tags:

ruby

gem

It is common to have a version.rb file, defining the version of the gem, like:

module Foo
  VERSION = "0.0.1"
end

and in the .gemspec file, require the version.rb and use Foo::VERSION there.

What is the benefit of this convention?

--

Also, sometime when namespace is used, the generated verison file can be deeply in nested folders. Wouldn't it be easier to place a version.rb directly under lib/ and specifying the version there?

like image 407
lulalala Avatar asked Apr 10 '14 04:04

lulalala


People also ask

What is Gemspec in Gemfile?

So It all started in 2006 when RubyGems created Gemspec to define Ruby package specifications. Gemspec is basically the Readme for gems. It tells you about the author, version, summary, description, internal dependencies, execution and everything about the Gem.

How do I check RubyGems version?

If this is the case, you can look on /usr/lib/ruby/gems/1.8/gems/rubygems-update-x.x.x and take the directory with the highest value of x.x.x.

What does gem update do?

Updating a Gem Without Modifying the Gemfile This command will update rack-cache and its dependencies to the latest version allowed by the Gemfile (in this case, the latest version available). It will not modify any other dependencies.


3 Answers

This is done this way to reduce churn (change rate) on .gemspec file. I imagine that dependency changes are much more important than version bumps (which are also more frequent). So version changes would add too much noise and hide important commits.

like image 104
Sergio Tulentsev Avatar answered Oct 09 '22 05:10

Sergio Tulentsev


It depends on what gem builder you used. Having a separate file makes it easy to stomp and rebuild as you bump version numbers with rake tasks, for example. this is a lot easier than fiddling with a source file and trying not to damage anything.

Some packagers use a separate VERSION file.

like image 27
tadman Avatar answered Oct 09 '22 04:10

tadman


In short, if your gem doesn't need to know the current version itself, then you don't need a version.rb. Even some of the well-known gems like rack don't use it.

Let's say you are building an executable gem that contains an option -v to see the current version, or you have to print current version to warn developers there are some deprecated changes, then to require a separated version file is much more convenient and efficient than parse the gemspec.

It's just a convention in Ruby community. The main reason of why version.rb is everywhere is that lots of gems are built by bundler gem.

like image 45
Weihang Jian Avatar answered Oct 09 '22 06:10

Weihang Jian