0

I'm trying to write a rake file to import data from csv, and I want to use smarter_csv gem. I have the gem installed globally (I don't want to add it to my Gemfile because it's a one-off task).

In my rake file I require 'smarter_csv' but when I run the task I get the following error:

rake aborted!
LoadError: cannot load such file -- smarter_csv

Every rake example I can find tells you to just require 'foo'. I can run the code manually in irb after requiring smarter_csv.

What am I missing?

(If it matters, I'm using rbenv on macOS Catalina)

jbmoon
  • 129
  • 1
  • 11
  • 1
    What do you think about adding the gem to you Gemfile with `require: false` option? In this case it won't be loaded until you require it explicitly. – Konstantin Strukov Sep 05 '20 at 05:31

2 Answers2

0

Rails uses bundler, and loads the gems mentioned in gemfile into memory. Doesn't look like you've any other option.

I had a similar use case, I tried putting it into a development group like so,

group :development do
  gem 'smarter_csv'
end
Kumar
  • 3,116
  • 2
  • 16
  • 24
0

If you do want to load a gem without having it in your Gemfile you can do the following in your rake task:

$: << '/Users/<user>/.rvm/rubies/ruby- 
x.x.x/lib/ruby/gems/x.x.x/gems/smarter_csv-x.x.x/lib'
require 'smarter_csv'

I think the option to have the gem in your Gemfile with the require: false option is more suitable however. The gem won't be loaded until you specifically require it.

benjessop
  • 1,729
  • 1
  • 8
  • 14
  • Yes, it was definitely a $LOAD_PATH thing. Here's what I did: path = "#{ENV["HOME"]}/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/smarter_csv-1.2.6/lib" $LOAD_PATH << path – jbmoon Sep 06 '20 at 14:10