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

4

3

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.

Icebreaker answered 15/5, 2012 at 14:9 Comment(1)
Do you autogenerate your gemspec?Terranceterrane
K
6

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.

Kendrickkendricks answered 13/2, 2013 at 21:15 Comment(1)
hmm, this doesn't work for me, do we need to add an explicit require as well?Bauer
U
4

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.

Unlay answered 2/2, 2015 at 19:4 Comment(0)
I
1

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
Icebreaker answered 15/5, 2012 at 15:43 Comment(0)
T
1

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.

Tappet answered 7/2, 2016 at 17:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.