7

I have a gem called "something".

I would like to add pry as a development dependency when developing the gem. However I don't know how to load it.

If I have "require something" inside lib/something.rb , when I release the gem, it throws a LoadError, because pry is only a development dependency.

At the same time I don't want to keep adding and removing pry when I am committing code.

What is the best way to require pry only when developing the application, but not require it as a dependency for the gem?

Anton Evangelatov
  • 1,397
  • 2
  • 14
  • 31
  • Similar question, but no answer: http://stackoverflow.com/questions/18109735/use-pry-in-gems-without-modifying-the-gemfile-or-using-require?rq=1 – Anton Evangelatov Aug 20 '13 at 12:37
  • 1
    Perhaps consider what context you are executing the code that needs pry available. If it's from specs then put the require in `spec_helper.rb` (or equivalent). If it's from a rake task then put it in the `Rakefile`. If it's from a raw script then put the require there or load it from command-line arguments with `ruby -rpry` – Abe Voelker Aug 20 '13 at 14:12

2 Answers2

3

You can use the add_development_dependency in the gemspec file. You'll still have to require it in your lib/something.rb file within a begin .. rescue LoadError block. (Edit 2, see below)

In your case, it will be something like the following:

spec.add_development_dependency 'pry', '~> 0.9.12.2'

The purpose of add_development_dependency is to separate the gems into dependencies that get installed when you execute gem install mygem vs development-only dependencies that are installed only when you execute gem install mygem --development.

Edit: @Pierre-Louis Gottfrois' solution

Modify the Gemfile directly and add a test group. This question describes the process. This does not appear to be a preferred solution according to Yehuda Katz.

Edit 2: begin require ... rescue LoadError is apparently a common practice for Ruby scripts, according to this Making Ruby Gems article.

Community
  • 1
  • 1
  • 1
    This works perfectly and is the way to go, however it will not work on test env. You'll need to add the gem in a `test` group in the `Gemfile` directly. – Pierre-Louis Gottfrois Aug 20 '13 at 12:53
  • However later when I release the gem in production, I still have this "require pry" in lib/something.rb , but "pry" is now missing as it is a development dependency. Before releasing I should remove "require pry" if I understand correctly... this is exactly what I want to avoid. – Anton Evangelatov Aug 20 '13 at 13:04
  • @antonevangelatov Maybe try a conditional `require` in a `begin .. rescue LoadError` block? Something like [this question](http://stackoverflow.com/questions/1835927/conditional-dependency-in-ruby-gemspec) –  Aug 20 '13 at 13:07
  • @user2062950 that would definitely work, but it feels like a hack. I was wondering if there is a "proper" way to do it, i.e. require/load certain gems (pry, debugger, etc.) only in development, without having to bother to include/exclude them prior to deployment/release. I don't understand what is the purpose of add_development_dependency, when you have to later add a "require" and also make sure you remove it, since the necessary gem is not loaded. If you are to manually add "require" every time, you might as well manually add the necessary gem to gemspec or Gemfile. – Anton Evangelatov Aug 20 '13 at 13:13
  • @antonevangelatov added more info –  Aug 20 '13 at 15:19
  • @user2062950 thanks for the thorough answer. I will mark it as correct, as there is no better solution apparently. I understand that add_development_dependency will install given gems only in development-mode, but I still have doubts on the best practice to "require"/load them... If I take "rspec" for example, it is quite clear - it is required only in "spec_helper" for example and therefore does not interfere with the rest of the app. One would only run rspec (spec_helper.rb) when developing... however the same does not apply to "pry" as I need it throughtout the whole app while developing. – Anton Evangelatov Aug 20 '13 at 17:23
0

I think I found a workaround for that. If you configure bundler to use pry as your console with

$ bundle config console pry

Then pry is itself required and you don't need to explicitly require in your source files.
Plus, you get a history on pressing ' ↑ '.

cmantas
  • 1,516
  • 14
  • 14