简体   繁体   English

开发gem时如何使用“调试器”和“撬开”? (红宝石)

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

I'm developing a gem, and my Gemfile looks like this: 我正在开发宝石,我的Gemfile如下所示:

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). 但是,我不希望人们在运行测试时必须安装prydebugger ,但我也希望能够在测试中要求它们(因为我正在运行以bundle exec为前缀的测试,而我无法获得它们)如果它们不在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? 另外,什么时候将我用于开发的宝石放在gemspec中,什么时候将它们放在Gemfile中? I really don't know the difference. 我真的不知道区别。

You can add gems to your gemspec as a development dependency, like this: 您可以将gems作为开发依赖项添加到gemspec中,如下所示:

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. 仅当在gem上工作时才安装这些插件,而不是在安装gem本身时安装。

Just as @andrew-vit said, you can add it to your gem specifications as a development dependency 就像@ andrew-vit所说的那样,您可以将其作为开发依赖项添加到您的gem规范中。

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. 由于这将取决于开发,因此您不想在主gem应用程序中添加spec_helper.rb require 'pry' ,因此只需将其添加到spec_helper.rb或任何测试安装文件中即可。

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. 然后,当您运行规格时,仍然可以将binding.pry添加到沙箱的代码中。 Make sure your specs run before pushing your code. 在推送代码之前,请确保您的规范已运行。 That way if it breaks, you'll see you forgot a breakpoint in your code. 这样,如果中断,您将看到忘记了代码中的断点。

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

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: 我当前正在使用ruby 2.2.4并将其添加到.gempsec中:

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

and this for my spec_helper.rb : 这是我的spec_helper.rb

require 'pry'

Not sure if that helps anyone. 不确定是否可以帮助任何人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM