Search code examples
rubypry

How to exit the loop in pry


If you keep type 'n',

you will be in the loop for 100 times,

How could I leave the each loop and continue to keep debugging from line 7 without exiting the loop then run the remain code automatically,

The behavior of exit !!! are not suit for me, Because I want to keep debugging the code after I exit the loop.

    1: require 'pry'
    2:
 => 3: binding.pry
    4: (1..100).each do |x|
    5:     print x
    6: end
    7:
    8: print "hi"

Solution

  • Use gem install pry-debugger (if you have installed pry-nav you may uninstall it at first).When you come here:

        1: require 'pry'
        2: 
     => 3: binding.pry
        4: (1..100).each do |x|
        5:   print x
        6: end
        7: 
        8: print "hi"
    

    Set a breakpoint at line 8 by using break 8:

    [2] pry(main)> break 8
    Breakpoint 1: /home/darfux/Desktop/ruby/STO/23622590.rb @ line 8 (Enabled) :
    
        5:   print x
        6: end
        7: 
     => 8: print "hi"
    

    Then type continue to continue the program and it will hit the breakpoint at line 8:

    [3] pry(main)> continue
    123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
    Breakpoint 1. First hit.
    
    From: /home/darfux/Desktop/ruby/STO/23622590.rb @ line 8 :
    
        3: binding.pry
        4: (1..100).each do |x|
        5:   print x
        6: end
        7: 
     => 8: print "hi"
    
    [4] pry(main)> 
    

    See more here