Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gem not loading: NameError (uninitialized constant) | But gem works in local path

I got a new machine and installed rbenv fresh (I always used rvm before). This gem now won't load in my app. I made no code changes. The only difference between now and then is 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)

Interestingly, if I clone the gem to my local machine, then change the path in the Gemfile, it DOES load/work.

# gem 'sports_api', git: '[email protected]:mikesilvis/sports_api.git'
gem 'sports_api', path: '~/dev/sports_api'


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

Why does it work on local path but not on the git path? Any ideas on what I can do to fix this (to use the git path)? Thank you in advance.

Edit: Here is the full backtrace.

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>'
like image 405
iamse7en Avatar asked Nov 12 '18 09:11

iamse7en


2 Answers

Updated:

There's a trip made by gem sports_api.

At the first line of lib/sports_api.rb: reject { |file| file.match(/version/) } the block would filter all the ruby files. Because of rbenv install ruby into ~/.rbenv/versions directory, with that the path of every gem files would come along with version string:

Each Ruby version is installed into its own directory under ~/.rbenv/versions.


Old answer:

Remove reject method at the first line of sports_api.rb:

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

to:

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

then you should delete cached gem files installed by bundler and run bundle install

like image 93
Cong Chen Avatar answered Oct 19 '22 22:10

Cong Chen


Run rbenv init and follow instructions. Note, restarting terminal is require after installing rbenv (see https://github.com/rbenv/rbenv#installation for details).

Ensure gem installed in rbenv path by executing bundle show sports_api.

like image 1
Tema Bolshakov Avatar answered Oct 19 '22 20:10

Tema Bolshakov