4

I'm trying to debug a simple ruby console script and am getting a load error when trying to require pry:

  • I'm using rbenv to management environment.
  • I'm using Ruby version: 2.3.1.
  • Trying to use Pry '~> 0.10.4'

/Users/gangelo/.rbenv/versions/2.3.1/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- pry (LoadError)

I've used Pry and Byebug in the past in rails applications and never had any issues like this. Searching for a solution, I've found that most issues are related to either not including Pry in the Gemfile or not including the Pry gem in the correct environment in the Gem file; this isn't the case with me, what am I doing wrong?

# /Gemfile
group :development, :test do
  gem 'pry', '~> 0.10.4'
end

And in my script:

# /calculator/rpn_calculator_service.rb
module RealPage
  module Calculator
    # Provides Reverse Polish Notation computation services.
    class RPNCalculatorService < CalculatorService
      include Helpers::Arrays

      def initialize
        super RPNInputParser.new
      end

      def compute(input)
        # Load error here :(
        require 'pry'; binding.pry
        # Code removed for brevity...
      end
      # Code removed for brevity...
    end
  end
end
gangelo
  • 3,034
  • 4
  • 29
  • 43
  • 1
    `bundle exec pry` – Aleksei Matiushkin Jan 07 '18 at 12:51
  • @mudasobwa Thank you. There no way to simply break into the session using binding.pry like you would in a rails app for instance? – gangelo Jan 07 '18 at 12:54
  • I am not sure I follow. Install `pry` globally and break wherever you want, or force the gemset to be loaded. `rails` behaves the same. – Aleksei Matiushkin Jan 07 '18 at 12:56
  • 1
    @gangelo You don't need to require 'pry' inside a class. Rails automatically require all gems listed in gem file when boot up. As you can see rails documentation inside `application.rb`. `# Require the gems listed in Gemfile, including any gems # you've limited to :test, :development, or :production. Bundler.require(*Rails.groups)` I think it is not good practice to specify version of gem. All require statements should be at top of the class. – TheVinspro Jan 07 '18 at 13:06
  • @TheVinspro - I'm not using rails. – gangelo Jan 07 '18 at 13:18
  • @mudasobwa - I don't want to install pry globally and as far as "force[ing] the gemset to be loaded" I'm using rbenv not rvm. If by that you mean, just including it in my Gemfile, I've done that. I'm relatively new to ruby and rails, so if you mean something else please explain. – gangelo Jan 07 '18 at 13:23
  • 2
    To use the gemset one should issue `bundle exec script`, not just `script`. – Aleksei Matiushkin Jan 07 '18 at 13:27
  • @mudasobwa - ah, I got you. I'm doing that. Now apparently I need to install additional gem plugins to navigate to my breakpoints? This is not that hard setting up rails, I guess pry-rails does all that for you :S – gangelo Jan 07 '18 at 13:32
  • @mudasobwa - installed pry-byebug - gives me everything I need, then put binding.pry where needed, execute $ bundle exec pry -r ./my_script.rb, and use the next command to hit my breakpoints. Thank you. Submit an official answer and I'll mark it. – gangelo Jan 07 '18 at 16:08

2 Answers2

7

I was receiving a similar error trying to run my gem cars:

/Users/giovanni/.rvm/rubies/ruby-2.2.3/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- pry (LoadError)
    from /Users/giovanni/.rvm/rubies/ruby-2.2.3/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from /Users/giovanni/.rvm/gems/ruby-2.2.3/gems/cars-0.0.6/bin/cars:3:in `<top (required)>'
    from /Users/giovanni/.rvm/gems/ruby-2.2.3/bin/cars:23:in `load'
    from /Users/giovanni/.rvm/gems/ruby-2.2.3/bin/cars:23:in `<main>'
    from /Users/giovanni/.rvm/gems/ruby-2.2.3/bin/ruby_executable_hooks:15:in `eval'
    from /Users/giovanni/.rvm/gems/ruby-2.2.3/bin/ruby_executable_hooks:15:in `<main>'

I deducted that maybe my gem was not installed so I just ran:

➜  ~ gem install pry
Fetching: coderay-1.1.2.gem (100%)
Successfully installed coderay-1.1.2
Fetching: method_source-0.9.0.gem (100%)
Successfully installed method_source-0.9.0
Fetching: pry-0.11.3.gem (100%)
Successfully installed pry-0.11.3
3 gems installed

And then executed my gem as usual

G. I. Joe
  • 1,585
  • 17
  • 21
  • Yes, I could have done that, but I didn't want to install it globally. Not sure why I couldn't simply use it as part of my bundled gems. – gangelo Mar 06 '18 at 20:47
0

These steps from bundler.io worked for me:

  1. Update to the latest version of bundler: gem install bundler

  2. Try to install one more time: bundle install

Mosab Sasi
  • 1,130
  • 8
  • 11