How do I step out of a loop with Ruby Pry?

Ruby on-RailsRubyLoopsExitPry

Ruby on-Rails Problem Overview


I'm using Pry with my Rails application. I set binding.pry inside a loop in my model to try and debug a problem. For example:

(1..100).each do |i|
  binding.pry
  puts i
end

When I type quit, it goes to the next iteration and stops again. Is there a way to step out of the loop so I don't have to type quit 100 times?

Currently the only way I know how to get out of it is to use CTRL+C and restart the application.

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

To exit Pry unconditionally, type

exit-program

Edit from @Nick's comment: Also works:

!!!

Solution 2 - Ruby on-Rails

I use:

disable-pry

This will keep the program running, but will keep it from continuing to stop execution. This is especially helpful when you are debugging in the console.

Solution 3 - Ruby on-Rails

To exit everything, use:

exit!

This should ignore all proceeding bindings.

Solution 4 - Ruby on-Rails

Triple exclamation (!!!) would do that.

Solution 5 - Ruby on-Rails

Use

disable-pry

To renable, add this to your controller

ENV['DISABLE_PRY'] = nil

Solution 6 - Ruby on-Rails

A binding.pry statement is exactly the same as a breakpoint in GDB. Such a breakpoint in GDB would be hit 100 times too.

If you only want the binding.pry to be hit once, for the first iteration of the loop, then use a conditional on the binding.pry like so:

(1..100).each do |i|
  binding.pry if i == 1
  puts i
end

You then exit the current session by just typing exit.

Solution 7 - Ruby on-Rails

Using gem pry-moves you can step out of loop using f (finish command)


example:

    42: def test
    43:   3.times do |i|
 => 44:     binding.pry
    45:     puts i
    46:   end
    47:   puts :finish
    48: end

[1] pry(main)> f
0
1
2

Frame: 0/1 method
From: playground/sand.rb:47 main

    42: def test
    43:   3.times do |i|
    44:     binding.pry
    45:     puts i
    46:   end
 => 47:   puts :finish
    48: end

Solution 8 - Ruby on-Rails

press 'q' and you will see just like this

[1] pry(#<AlbumsController>)>

type

exit

this one word will do, if not:

control + c

Solution 9 - Ruby on-Rails

Based on the two previous answers above:

Thank you guys! Your advices have helped me really a lot!

I just want to share a simple stupid trick, that I personally use to don't worry about the DISABLE_PRY environment variable all the time. Add this callback to the base controller ApplicationController of your project permanently. It would automatically re-enable PRY every time the disable-pry is called:

# app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
  before_action :reenable_pry

  private

  def reenable_pry
    ENV['DISABLE_PRY'] = nil
  end
end

Solution 10 - Ruby on-Rails

If you just need to debug one iteration, you can just raise error, escape guarantee :

(1..100).each do |i|
  binding.pry
  raise
  puts i
end

Or with condition :

(1..100).each do |i|
  if i == 50
    binding.pry 
    raise
  end
  puts i
end

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRyanView Question on Stackoverflow
Solution 1 - Ruby on-RailsEvandroView Answer on Stackoverflow
Solution 2 - Ruby on-RailsstebooksView Answer on Stackoverflow
Solution 3 - Ruby on-RailsBlakeView Answer on Stackoverflow
Solution 4 - Ruby on-RailsHahnView Answer on Stackoverflow
Solution 5 - Ruby on-RailsChet3x16View Answer on Stackoverflow
Solution 6 - Ruby on-RailshorseyguyView Answer on Stackoverflow
Solution 7 - Ruby on-RailsDaniel GarmoshkaView Answer on Stackoverflow
Solution 8 - Ruby on-RailsHeartless VayneView Answer on Stackoverflow
Solution 9 - Ruby on-RailszinovyevView Answer on Stackoverflow
Solution 10 - Ruby on-RailsMaxime BouéView Answer on Stackoverflow