我有一台新机器并且安装了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>'
解决方法
更新:
宝石sports_api有一次旅行.
在lib/sports_api.rb的第一行:
拒绝{|文件| file.match(/ version /)}该块将过滤所有ruby文件.因为rbenv将ruby安装到〜/ .rbenv / versions目录中,每个gem文件的路径都会随版本字符串一起出现:
Each Ruby version is installed into its own directory under
~/.rbenv/versions
.
老答案:
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) }