0

I'm writing a simple ruby application(basically using PORO's). I've added a Gemfile to it and I'm trying to require the pry gem(which is useful for adding breakpoints while debugging, as the application grows) via the Gemfile but I'm unable to require that gem using Bundler.setup, things work fine with Bundler.require.

I'm trying to avoid the use of Bundler.require for reasons stated in this blog for instance.

In the Employee.rb file, I have the below code -

# require 'bundler'
# require 'bundler/setup'
# Bundler.setup(:default, :test, :development)

Bundler.require(:default, :development, :test)

def total_score(scores)
  binding.pry #added on purpose , just to see if the app stops at this breakpoint
  scores.inject(:+)
end

When I use Bundler.setup(uncommenting the lines commented above), instead of Bundler.require, I get the below error for the command rspec spec . given from my apps root directory-

 Failure/Error: expect(total_score(scores)).to eq(16)
 NoMethodError:
   undefined method `pry' for #<Binding:0x007fbda22b79d0>
 # ./Employee.rb:9:in `total_score'
 # ./spec/employee_spec.rb:5:in `block (3 levels) in <top (required)>'

Also, just one more note, as per the bundler docs, I tried even doing a require rubygems, but even that doesn't help. Ideally, I would want to avoid using require rubygems for reasons mentioned in this link.

How do I get my app to require the pry gem using Bundler.setup, without doing a require rubygems alongside and also without using Bunder.require ?

boddhisattva
  • 6,908
  • 11
  • 48
  • 72

1 Answers1

5

Bundler.setup will simply add the gems to the load path, whereas Bundler.require will do both the adding to the load path and the require.

I recommend using Bundler.setup so that your code boots faster. Then when you need to require pry, do this:

require 'pry'
Ryan Bigg
  • 106,965
  • 23
  • 235
  • 261
  • I think a caveat to this currently is, as the app grows, I will have to require each of those gems that I'd want to use if I want to avoid a `Bundler.require` , in this case would there be a value in adding a Gemfile to my app? – boddhisattva Feb 17 '15 at 03:04
  • By having a Gemfile + Gemfile.lock, you can lock down to specific versions of the gems. If you don't have Gemfile + Gemfile.lock, different machines might run different Ruby versions. – Ryan Bigg Feb 17 '15 at 03:05
  • Hmm, Thanks Ryan, just one more thing, I would want to limit the usage of `pry` or similar gems like `rspec` etc., to only dev and test environments. By doing a `require pry` and a similar thing for other gems, I think these gems wouldn't be limited to the environments to which I'd want to limit their usage , right ? – boddhisattva Feb 17 '15 at 03:07