Janko
Janko

Reputation: 9335

How to use 'debugger' and 'pry' when developing a gem? (Ruby)

I'm developing a gem, and my Gemfile looks like this:

source :rubygems

gemspec

group :development, :test do
  gem "pry"
  gem "debugger"
  gem "rake"
end

However, I don't want people to have to install pry and debugger when running tests, but I also want to be able to require them in my tests (because I'm running tests prefixed with bundle exec, and I cannot get it them in my load path if they're not in the Gemfile). How do I achieve this?

Also, when to put gems that I use for development in the gemspec, and when to put them in the Gemfile? I really don't know the difference.

Upvotes: 3

Views: 3354

Answers (4)

sirko
sirko

Reputation: 143

For me a mix of the answers above seems to work. I'm currently running with ruby 2.2.4 and with adding this to the .gempsec:

s.add_development_dependency 'pry-byebug'
s.add_development_dependency 'rake'

and this for my spec_helper.rb:

require 'pry'

Not sure if that helps anyone.

Upvotes: 1

jeremywoertink
jeremywoertink

Reputation: 2341

Just as @andrew-vit said, you can add it to your gem specifications as a development dependency

Gem::Specification.new do |spec|
   #...
   spec.add_development_dependency "pry"
   spec.add_development_dependency "debugger"
   #...
end

Since this will be a development dependency, you don't want to add require 'pry' into your main gem app, so just add it into your spec_helper.rb or whatever your test setup file is.

require 'rspec'
require 'vcr'
require 'pry'
#...

Then when you run your specs, you can still add your binding.pry into the code for your sandbox. Make sure your specs run before pushing your code. That way if it breaks, you'll see you forgot a breakpoint in your code.

Upvotes: 4

Andrew Vit
Andrew Vit

Reputation: 19249

You can add gems to your gemspec as a development dependency, like this:

Gem::Specification.new do |s|
  # ...
  s.add_development_dependency 'pry'
  s.add_development_dependency 'debugger'
  s.add_development_dependency 'rake'
end

These will only be installed when working on the gem and not when installing the gem itself.

Upvotes: 6

Janko
Janko

Reputation: 9335

I found a solution. I can just put them in groups.

source :rubygems

gemspec

group :development, :test do
  gem "rake"
end

gem "debugger", :group => :debugger
gem "pry",      :group => :pry

This way the contributor can choose to not install them:

bundle install --without debugger pry

Upvotes: 1

Related Questions