How to use 'debugger' and 'pry' when developing a gem? (Ruby)
You can add gems to your gemspec as a development dependency, like this:
Gem::Specification.new do |s|
# ...
s.add_development_dependency 'pry'
s.add_development_dependency 'debugger'
s.add_development_dependency 'rake'
end
These will only be installed when working on the gem and not when installing the gem itself.
How to add pry when developing a Ruby gem
You can use the add_development_dependency
in the gemspec
file. You'll still have to require
it in your lib/something.rb
file within a begin .. rescue LoadError
block. (Edit 2, see below)
In your case, it will be something like the following:
spec.add_development_dependency 'pry', '~> 0.9.12.2'
The purpose of add_development_dependency
is to separate the gems into dependencies that get installed when you execute gem install mygem
vs development-only dependencies that are installed only when you execute gem install mygem --development
.
Edit: @Pierre-Louis Gottfrois' solution
Modify the Gemfile
directly and add a test
group. This question describes the process. This does not appear to be a preferred solution according to Yehuda Katz.
Edit 2: begin require ... rescue LoadError
is apparently a common practice for Ruby scripts, according to this Making Ruby Gems article.
How to add debugger (binding.pry) in dependecy gem?
No need for forking here. You can edit locally installed gems. Just do this:
bundle open kaminari
It will open the corresponding version of kaminari in your $EDITOR
. There you can insert debug printing, binding.pry
or whatever you like. Don't forget to restart the app/server.
And don't forget to clean up when you're done. Either by manually undoing your changes or running
bundle pristine kaminari
Ruby and Pry (or other debugging gem): How can I show the definition of a variable?
I think this is the closest I can come up with right now to answer your question. Please check pry-moves
You could use it to debug the start-up of the execution of your program and use watch variable
(if you at least know the name of the variable) and look when it gets a value, there you would know in which step of the execution it is defined/declared.
Is there any way to set a break point for debug in the environment in which gems like binding.pry are not installed?
Unfortunately, Ruby doesn't really have a built-in breakpoint-based debugger in that sense.
The closest thing you can do is start an IRB session mid-program, which halts it and allows you to inspect the current state of the program by evaluating Ruby commands. This is available in Ruby 2.4 and above as binding.irb
. (There are docs available for this on the Binding class.)
IRB doesn't let you step forwards like Pry with Pry-Nav does, but you can just insert binding.irb
expressions in as many places as you need, and exit
from IRB to "step" to the next one.
Take ths simple program. I want to check the value of those three variables I've assigned, so I put binding.irb
there:
puts "=== Hello! ==="
a = 1
b = 2
c = 3
binding.irb
puts "=== Goodbye! ==="
Upon running this program, Ruby will give you an IRB prompt after it's evaluated c = 3
, where you can run Ruby code to check values and whatnot (type exit
to leave):
=== Hello! ===
From: test.rb @ line 7 :
2:
3: a = 1
4: b = 2
5: c = 3
6:
=> 7: binding.irb
8:
9: puts "=== Goodbye! ==="
irb(main):001:0> a
=> 1
irb(main):002:0> b
=> 2
irb(main):003:0> c
=> 3
irb(main):004:0> exit
=== Goodbye! ===
Process for debugging a third-party gem
Use :path
in the project's gemfile.
You are working on gem my_gem
and project my_project
at the same time. To make this painless use this in the gemfile of your project
# in /path/to/my_project/Gemfile
...
gem 'my_gem', :path => '/path/to/my_gem'
...
And then you can iterate without having to build the gem all time.
There's isn't even a need to rename the gem.
How to correctly set up Pry in Rails 4.2
Some fiddling around and a couple of server restarts appear to have fixed the issue. Thanks to Deivid for suggesting remvoving pry-stack_explorer
. Here's the Gemfile for the test and development groups:
group :development do
gem 'better_errors', '~> 2.1.1'
gem 'annotate', '~> 2.6.10'
end
group :development, :test do
gem 'pry-rails'
gem 'pry-byebug'
gem 'web-console', '~> 2.0'
gem 'spring'
gem 'spring-commands-rspec', '~> 1.0.4'
gem 'rspec-rails', '~> 3.2.3'
gem 'guard-rspec', '~> 4.6.0'
gem 'sqlite3'
gem 'factory_girl_rails', '~> 4.5.0', require: false
end
group :test do
gem 'database_cleaner', '~> 1.4.1'
gem 'faker', '~> 1.4.3'
gem 'capybara', '~> 2.4.4'
gem 'launchy', '~> 2.4.3'
gem 'shoulda', '~> 3.5.0'
end
How can I debug a gem in Rails?
The answer is IN the stack trace. It's telling you exactly what file ON your system is having the error. The fact that that file came from a gem, or lives in a different part of your computer, is irrelevant.
/home/david-vm/.rvm/gems/ruby-2.3.0/gems/tiny_tds-0.7.0/lib/tiny_tds/client.rb:74:in `connect': Server name not found in configuration files (TinyTds::Error)
Open that file (or whatever file in the backtrace seems most appropriate) with your editor, stick a binding.pry
(or byebug, or debugger, whatever you're using) in there before the line it's blowing up on (74). Then restart your app and debug as if it was a file in your app.
Related Topics
Selenium Webdriver Take Screenshot of Viewport Only
My Classes Can't Use Shoes Methods Like Para
Error: Missing Rvm Environment File After Doing Rvm Upgrade Command - Passenger 4.0.23
Vps Apache Config - Invalid Command 'Passengerdefaultruby' After Adding Latest Passenger Gem
Rexml::Document.New How to Give Encode Parameters on This Line
Invalid Route Name, Already in Use: 'Admin_Root' (Argumenterror) - Failed Activeadmin Install
Using --No-Rdoc and --No-Ri with Bundler
Ide Sublime2 How to Find Method Definition
Connection Refused Using Sunspot and Solr in Rails
Editing JSON Array Contents in Ruby
Selenium Webdriver & Chrome Driver - Not Able to Run Chrome Driver
Save Google Cloud Speech API Operation(Job) Object to Retrieve Results Later
How to Create Nested Model from Partial Rails 6
Pulling a Value from One CSV Based on a Value in Another
Boot Up Rails App, Make Request to App from Outside Local Network