gem list to gemfile

Say you have n-machines that needs to be in sync with their ruby gem versions, we can have one system to update all as per need and then create this Gemfile and distribute the same.

Here is a simple chunk of code that would help to convert all the gems install to a gemfile, which can later be used with bundler.

def make_gemfile
  [:each_default,:each_normal].flat_map { |sym|  
    Gem::Specification.to_enum(sym).map{|spec| 
    "gem \"#{spec.name}\", :version => \"#{spec.version}\""
    } 
  }
end

On invocation, you will see something like :

gem "bigdecimal", :version => "1.2.0"
gem "io-console", :version => "0.4.2"
gem "json", :version => "1.7.7"
gem "minitest", :version => "4.3.2"
gem "psych", :version => "2.0.0"
gem "rake", :version => "0.9.6"
gem "rdoc", :version => "4.0.0"
gem "test-unit", :version => "2.0.0.0"
gem "gita", :version => "0.0.1"
gem "howdoi", :version => "0.0.3"
gem "minitest", :version => "4.6.2"
gem "nokogiri", :version => "1.5.6"
gem "rubygems-update", :version => "2.0.2"
gem "test-unit", :version => "2.5.4"

P.S : Thanks to @hanmac for providing a better ruby hack, compared to my python parsing :D

Note :  [].flat_map(&blk) is same as [].map(&blk).flatten(1)

Share this