无法要求将宝石加载为add_development_dependency

时间:2018-10-16 21:47:17

标签: ruby-on-rails ruby rubygems

在mygem.gemspec中,我只有以下宝石,仅用于调试红宝石宝石-

Gem::Specification.new do |s|
  ...
  s.add_development_dependency "pry"
  ...
end

然后在我的Rails生成器文件中,我有require "pry",但是出现以下错误-

.rvm/gems/ruby-2.3.3/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:292:in `require': cannot load such file -- pry (LoadError)

如果我将add_development_dependency更改为add_dependency,它将起作用。我在做错什么吗?

编辑:

lib / generators / mygem / install_generator.rb

require "pry"

module MyGem
  module Generators
    class InstallGenerator < Rails::Generators::Base

      namespace "mygem:install"

      def process
        binding.pry
      end

    end
  end
end

完整堆栈跟踪

$ rails g mygem:install
Running via Spring preloader in process 99478
.rvm/gems/ruby-2.3.3/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:292:in `require': cannot load such file -- pry (LoadError)
  from .rvm/gems/ruby-2.3.3/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:292:in `block in require'
  from .rvm/gems/ruby-2.3.3/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:258:in `load_dependency'
  from .rvm/gems/ruby-2.3.3/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:292:in `require'
  from Projects/mygem/gem/mygem/lib/generators/cast/install_generator.rb:1:in `<top (required)>'
  from .rvm/gems/ruby-2.3.3/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:292:in `require'
  from .rvm/gems/ruby-2.3.3/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:292:in `block in require'
  from .rvm/gems/ruby-2.3.3/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:258:in `load_dependency'
  from .rvm/gems/ruby-2.3.3/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:292:in `require'
  from .rvm/gems/ruby-2.3.3/gems/railties-5.1.6/lib/rails/command/behavior.rb:82:in `block (2 levels) in lookup'
  from .rvm/gems/ruby-2.3.3/gems/railties-5.1.6/lib/rails/command/behavior.rb:78:in `each'
  from .rvm/gems/ruby-2.3.3/gems/railties-5.1.6/lib/rails/command/behavior.rb:78:in `block in lookup'
  from .rvm/gems/ruby-2.3.3/gems/railties-5.1.6/lib/rails/command/behavior.rb:77:in `each'
  from .rvm/gems/ruby-2.3.3/gems/railties-5.1.6/lib/rails/command/behavior.rb:77:in `lookup'
  from .rvm/gems/ruby-2.3.3/gems/railties-5.1.6/lib/rails/generators.rb:250:in `find_by_namespace'
  from .rvm/gems/ruby-2.3.3/gems/railties-5.1.6/lib/rails/generators.rb:267:in `invoke'
  from .rvm/gems/ruby-2.3.3/gems/railties-5.1.6/lib/rails/commands/generate/generate_command.rb:24:in `perform'
  from .rvm/gems/ruby-2.3.3/gems/thor-0.20.0/lib/thor/command.rb:27:in `run'
  from .rvm/gems/ruby-2.3.3/gems/thor-0.20.0/lib/thor/invocation.rb:126:in `invoke_command'
  from .rvm/gems/ruby-2.3.3/gems/thor-0.20.0/lib/thor.rb:387:in `dispatch'
  from .rvm/gems/ruby-2.3.3/gems/railties-5.1.6/lib/rails/command/base.rb:63:in `perform'
  from .rvm/gems/ruby-2.3.3/gems/railties-5.1.6/lib/rails/command.rb:44:in `invoke'
  from .rvm/gems/ruby-2.3.3/gems/railties-5.1.6/lib/rails/commands.rb:16:in `<top (required)>'
  from .rvm/gems/ruby-2.3.3/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:292:in `require'
  from .rvm/gems/ruby-2.3.3/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:292:in `block in require'
  from .rvm/gems/ruby-2.3.3/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:258:in `load_dependency'
  from .rvm/gems/ruby-2.3.3/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:292:in `require'
  from Projects/mygem/themes/sample/bin/rails:9:in `<top (required)>'
  from .rvm/gems/ruby-2.3.3/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:286:in `load'
  from .rvm/gems/ruby-2.3.3/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:286:in `block in load'
  from .rvm/gems/ruby-2.3.3/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:258:in `load_dependency'
  from .rvm/gems/ruby-2.3.3/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:286:in `load'
  from .rvm/rubies/ruby-2.3.3/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:59:in `require'
  from .rvm/rubies/ruby-2.3.3/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:59:in `require'
  from -e:1:in `<main>'

编辑:

这是一个有问题的示例宝石-https://github.com/ryangrush/stackoverflow_ruby_gem

2 个答案:

答案 0 :(得分:2)

我认为您误解了“ development_dependency”的含义:(这与您的rails环境无关,因为大多数宝石都不依赖于rails或环境标志)

Taken From RubyGems Guide

  

声明依赖 ...

     

运行时与开发

     

RubyGems提供了两种主要的依赖性“类型”:运行时和开发。运行时依赖项是您的gem需要工作的内容(例如,需要activesupport的导轨)。

     

开发依赖项在有人要修改您的gem时很有用。当您指定开发依赖关系时,另一个开发人员可以运行 gem install --dev your_gem ,而RubyGems将获取两组依赖关系(运行时和开发)。典型的开发依赖项包括测试框架和构建系统。

     

在gemspec中设置依赖关系很容易。只需使用 add_runtime_dependency add_development_dependency

Gem::Specification.new do |s|
  s.name = "hola"
  s.version = "2.0.0"
  s.add_runtime_dependency "daemons", ["= 1.1.0"]
  s.add_development_dependency "bourne", [">= 0"]

因此,由于您的宝石需要pry才能正常工作,因此根据您的设计,pry将成为runtime_dependency。要么,要么您需要在此代码周围添加一些陷阱,以确定

  • a)是否定义了Rails?和;
  • b)我们是否处于development?环境中?和;
  • c)是否定义了Pry?要么;
  • d)我们可以加载pry(从LoadError救援)

答案 1 :(得分:1)

将此添加到项目根目录中的Gemfile中:

group :development, :test do
  gem 'pry'
end

当然可以运行

bundle install