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

75,818

Solution 1

To exit Pry unconditionally, type

exit-program

Edit from @Nick's comment: Also works:

!!!

Solution 2

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

To exit everything, use:

exit!

This should ignore all proceeding bindings.

Solution 4

Triple exclamation (!!!) would do that.

Solution 5

Use

disable-pry

To renable, add this to your controller

ENV['DISABLE_PRY'] = nil
Share:
75,818

Related videos on Youtube

Ryan
Author by

Ryan

Updated on May 19, 2021

Comments

  • Ryan
    Ryan about 3 years

    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.

  • Ryan
    Ryan almost 12 years
    Thanks @Evandro, that is exactly what I was looking for!
  • AJP
    AJP almost 12 years
    Though you'll obviously hit the binding straight away, using just exit will allow the program to keep running.
  • Eric Walker
    Eric Walker almost 11 years
    This did not work for me when running rspec. But CTRL-C, twice, did.
  • Ryan
    Ryan almost 11 years
    This also kills the server at the same time. exit-program is still probably the best option if you don't want to restart the server.
  • Alan David Garcia
    Alan David Garcia over 10 years
    Just to clarify exit-program allows you to maintain your rails server session but seems to throw a SystemExit
  • stebooks
    stebooks almost 10 years
    To re-enable (from the command line): ENV['DISABLE_PRY'] = nil
  • daslicious
    daslicious over 9 years
    i was not able to reenter pry after using disable-pry. Even after using ENV['DISABLE_PRY'] = nil
  • Topher Hunt
    Topher Hunt about 9 years
    on Mac OSX, pressing Ctrl + C twice will kill the Pry session, but will also effectively kill that terminal window: subsequent output is glitched such that I need to close that terminal tab and move to a new one. However !!! does not have this aggravating effect.
  • Radix
    Radix almost 9 years
    To reenter pry, all you need to do is to set ENV['DISABLE_PRY'] = nil in your controller not in command line OR rails console.
  • BBonifield
    BBonifield over 8 years
    This is the perfect one to use when you're using guard and just want it to stop running pry for that test run. It'll reset on the next test run.
  • Sri Harsha Kappala
    Sri Harsha Kappala about 8 years
    @Evandro You have no idea how this saved me!
  • SidOfc
    SidOfc about 8 years
    This was the solution for me on OSX 10.11.4 (El Capitan), I did not try the disable-pry + ENV['DISABLE_PRY'] = nil combo but I did try exit-program which gave me a systemExit. This one just works. (On rails 3.2.22.2, pry 0.10.3 and ruby 1.9.3)
  • Adamantish
    Adamantish almost 8 years
    Warning: If used inside an rspec test it can cause the test to falsely go green. Use the answer by @stebooks instead: disable-pry
  • vikramvi
    vikramvi about 7 years
    In case somebody struck with * icon shown on pry command with Mac terminal , do single ctrl + C to start using Pry again