2

For example, I have the following Gemfile:

source "https://rubygems.org"

group :example_group do
  gem 'example_gem'
  gem 'another_example_gem'
end

How can I list all gems inside :example_group in Ruby? Not really looking for a shell command.

Ace Subido
  • 86
  • 9

2 Answers2

1

Adapting from the Bundler::Graph, you could have :

Bundler.load.current_dependencies.select{|dep|
  dep.groups.include?(:example_group)}.map(&:name)
Pi Trem
  • 23
  • 4
0

Not sure what you are trying to do here, but some ideas:

  • Gemfile is ruby code, meaning you can write plain ruby there
  • the 'gem' method in Gemfile does return the current list of dependencies

So something like:

source "https://rubygems.org"

gems = []

group :example_group do
  gem 'example_gem'
  gems << gem 'another_example_gem'
end

group_gems = gems.select { |gm| gm.groups.include?(:example_group) }.map { |gm| gm.name }
p group_gems.uniq

Will print out your gems in the group when you use bundler (bundle install for example). Instead of printing them, you could save them somewhere.

Martin
  • 7,634
  • 1
  • 20
  • 23