gpt4 book ai didi

ruby-on-rails - 无法要求将 gem 加载为 add_development_dependency

转载 作者:太空宇宙 更新时间:2023-11-03 18:04:14 26 4
gpt4 key购买 nike

在我的 mygem.gemspec 中,我有以下 gem,我只想要它来调试我的 ruby​​ gem -

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>'

编辑:

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

最佳答案

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

Taken From RubyGems Guide :

DECLARING DEPENDENCIES...

Runtime vs. development

RubyGems provides two main “types” of dependencies: runtime and development. Runtime dependencies are what your gem needs to work (such as rails needing activesupport).

Development dependencies are useful for when someone wants to make modifications to your gem. When you specify development dependencies, another developer can run gem install --dev your_gem and RubyGems will grab both sets of dependencies (runtime and development). Typical development dependencies include test frameworks and build systems.

Setting dependencies in your gemspec is easy. Just use add_runtime_dependency and 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"]

因此,由于您的 gem 需要 pry 才能工作,根据您的设计,pry 将是一个 runtime_dependency。要么,要么你需要围绕这段代码设置一些陷阱来确定

  • a) 是否定义了 Rails?和;
  • b) 我们是否处于开发? 环境中?和;
  • c) 是否定义了Pry?或者;
  • d) 我们可以加载pry吗(从LoadError中拯救)

关于ruby-on-rails - 无法要求将 gem 加载为 add_development_dependency,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52844467/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com