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
9
  • 1
    bundle exec pry Commented Jan 7, 2018 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? Commented Jan 7, 2018 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. Commented Jan 7, 2018 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. Commented Jan 7, 2018 at 13:06
  • 2
    To use the gemset one should issue bundle exec script, not just script. Commented Jan 7, 2018 at 13:27

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

1 Comment

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.
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.