宝石未加载:NameError(未初始化的常量)但是宝石在当地道路上工作

时间:2018-11-12 09:27:44

标签: ruby-on-rails ruby rubygems rbenv gemfile

我有一台新机器,并重新安装了rbenv(我以前一直使用rvm)。 This gem现在不会加载到我的应用中。我没有更改代码。现在和那时之间的唯一区别是rbenv。

Loading development environment (Rails 5.2.1)
irb(main):001:0> SportsApi::Fetcher::Score::NBA
Traceback (most recent call last):
        1: from (irb):1
NameError (uninitialized constant SportsApi::Fetcher)

有趣的是,如果我将gem克隆到本地计算机,然后更改Gemfile中的路径,则它确实可以加载/工作。

# gem 'sports_api', git: 'git@github.com:mikesilvis/sports_api.git'
gem 'sports_api', path: '~/dev/sports_api'


irb(main):001:0> SportsApi::Fetcher::Score::NBA
=> SportsApi::Fetcher::Score::NBA

为什么它不能在本地路径上工作,而不能在git路径上工作?关于如何解决此问题(使用git路径)的任何想法?预先谢谢你。

编辑:这是完整的回溯。

uninitialized constant SportsApi::Fetcher

/Users/myusername/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/core_ext/active_support.rb:74:in `block in load_missing_constant'
/Users/myusername/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/core_ext/active_support.rb:8:in `without_bootsnap_cache'
/Users/myusername/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/core_ext/active_support.rb:74:in `rescue in load_missing_constant'
/Users/myusername/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/core_ext/active_support.rb:56:in `load_missing_constant'
(irb):2:in `irb_binding'
/Users/myusername/.rbenv/versions/2.5.3/lib/ruby/2.5.0/irb/workspace.rb:85:in `eval'
/Users/myusername/.rbenv/versions/2.5.3/lib/ruby/2.5.0/irb/workspace.rb:85:in `evaluate'
/Users/myusername/.rbenv/versions/2.5.3/lib/ruby/2.5.0/irb/context.rb:380:in `evaluate'
/Users/myusername/.rbenv/versions/2.5.3/lib/ruby/2.5.0/irb.rb:491:in `block (2 levels) in eval_input'
/Users/myusername/.rbenv/versions/2.5.3/lib/ruby/2.5.0/irb.rb:623:in `signal_status'
/Users/myusername/.rbenv/versions/2.5.3/lib/ruby/2.5.0/irb.rb:488:in `block in eval_input'
/Users/myusername/.rbenv/versions/2.5.3/lib/ruby/2.5.0/irb/ruby-lex.rb:246:in `block (2 levels) in each_top_level_statement'
/Users/myusername/.rbenv/versions/2.5.3/lib/ruby/2.5.0/irb/ruby-lex.rb:232:in `loop'
/Users/myusername/.rbenv/versions/2.5.3/lib/ruby/2.5.0/irb/ruby-lex.rb:232:in `block in each_top_level_statement'
/Users/myusername/.rbenv/versions/2.5.3/lib/ruby/2.5.0/irb/ruby-lex.rb:231:in `catch'
/Users/myusername/.rbenv/versions/2.5.3/lib/ruby/2.5.0/irb/ruby-lex.rb:231:in `each_top_level_statement'
/Users/myusername/.rbenv/versions/2.5.3/lib/ruby/2.5.0/irb.rb:487:in `eval_input'
/Users/myusername/.rbenv/versions/2.5.3/lib/ruby/2.5.0/irb.rb:428:in `block in run'
/Users/myusername/.rbenv/versions/2.5.3/lib/ruby/2.5.0/irb.rb:427:in `catch'
/Users/myusername/.rbenv/versions/2.5.3/lib/ruby/2.5.0/irb.rb:427:in `run'
/Users/myusername/.rbenv/versions/2.5.3/lib/ruby/2.5.0/irb.rb:383:in `start'
/Users/myusername/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/railties-5.2.1/lib/rails/commands/console/console_command.rb:64:in `start'
/Users/myusername/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/railties-5.2.1/lib/rails/commands/console/console_command.rb:19:in `start'
/Users/myusername/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/railties-5.2.1/lib/rails/commands/console/console_command.rb:96:in `perform'
/Users/myusername/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/thor-0.20.3/lib/thor/command.rb:27:in `run'
/Users/myusername/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/thor-0.20.3/lib/thor/invocation.rb:126:in `invoke_command'
/Users/myusername/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/thor-0.20.3/lib/thor.rb:387:in `dispatch'
/Users/myusername/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/railties-5.2.1/lib/rails/command/base.rb:65:in `perform'
/Users/myusername/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/railties-5.2.1/lib/rails/command.rb:46:in `invoke'
/Users/myusername/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/railties-5.2.1/lib/rails/commands.rb:18:in `<main>'
/Users/myusername/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `require'
/Users/myusername/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `block in require_with_bootsnap_lfi'
/Users/myusername/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/loaded_features_index.rb:65:in `register'
/Users/myusername/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:20:in `require_with_bootsnap_lfi'
/Users/myusername/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:29:in `require'
bin/rails:4:in `<main>'

4 个答案:

答案 0 :(得分:2)

已更新:

sports_api是宝石之旅。

lib/sports_api.rb的第一行: reject { |file| file.match(/version/) }块将过滤所有红宝石文件。由于rbenv将ruby安装到~/.rbenv/versions目录中,因此每个gem文件的路径都将带有version字符串:

  

每个Ruby版本都安装在~/.rbenv/versions下的自己的目录中。


旧答案:

reject第一行中删除sports_api.rb方法:

Dir["#{File.dirname(__FILE__)}/sports_api/**/**/*.rb"].sort_by(&:length).reject { |file| file.match(/version/) }.each { |f| load(f) }

收件人:

Dir["#{File.dirname(__FILE__)}/sports_api/**/**/*.rb"].sort_by(&:length).each { |f| load(f) }

然后,您应该删除捆绑程序安装的缓存的gem文件并运行bundle install

答案 1 :(得分:1)

运行rbenv init并按照说明进行操作。请注意,安装rbenv后需要重启终端(有关详细信息,请参见https://github.com/rbenv/rbenv#installation)。

通过执行bundle show sports_api,确保在rbenv路径中安装了gem。

答案 2 :(得分:0)

我看到与asdf(vs rbenv)相同的行为,所以这不是rbenv问题。

在调用代码之前,在代码库中的某个位置添加require 'sports_api',同时使用您希望使用的语法,将解决加载问题。

我不确定100%的根本原因,但我怀疑这可能是路径/ git语法处理方式的组合。使用路径语法似乎会自动为您提供代码(我不希望如此)

答案 3 :(得分:0)

我在其他宝石上遇到了同样的问题,并通过重新启动spring解决了这个问题:

bin/spring stop

spring有助于更快地加载环境,但有时会遇到缓存问题。