sixty4bit sixty4bit - 1 year ago 74
Ruby Question

Why do I get NameError with `bundle exec ruby [file]` given that the necessary gem is in Gemfile?

I'm doing some messing around to try to understand better how bundler works. I have just three files in my working directory:

Gemfile Gemfile.lock test.rb

All the Gemfile has is
gem "slop"
looks like this:

puts Slop.parse

When I run
bundle exec test.rb
I get a
due to not having a

[ec2-user@xx my_app]$ bundle exec ruby test.rb
test.rb:1:in `<main>': uninitialized constant Slop (NameError)

But if I run
bundle console
, Bundler loads the gem correctly and I can run
from the console without having to explicitly type
require "slop"

[ec2-user@xx my_app]$ bundle console
irb(main):001:0> Slop.parse
=> #<Slop::Result:0x00000001339838...

So what am I missing? I was under the impression that since I don't have
require: false
in my
should be loaded when I run
bundle exec ruby test.rb
and I shouldn't need to put the
require "slop"
line in the file.

Answer Source

You need to config bundler to require all gems on your Gemfile like this:

require 'rubygems'
require 'bundler/setup'

Check the docs at

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download