Search code examples
ruby-on-railsrubybundlergemfile

How can I view all the gems for a specific gem group?


I'd like to see all the gems that are loaded for the test environment, with versions and dependencies. Is such a thing possible?


Solution

  • You could roll your own:

    require 'bundler/setup'
    
    group = :development
    
    deps = Bundler.load.dependencies.select do |dep|
      dep.groups.include?(group) or dep.groups.include?(:default)
    end
    
    puts "Gems included by the bundle in group #{group}:"
    deps.each do |dep|
      spec = dep.to_spec
      puts "* #{spec.name} (#{spec.version})"
    end
    

    Example Gemfile:

    source 'https://rubygems.org'
    
    
    gem 'sinatra'
    gem 'thor'
    
    group :test do
      gem 'rspec'
    end
    
    group :development do
      gem 'rspec'
      gem 'pry'
    end
    

    Example output:

    Gems included by the bundle in group development:
    * sinatra (1.4.1)
    * thor (0.17.0)
    * rspec (2.13.0)
    * pry (0.9.12)