How to Step Out of a Loop with Ruby Pry

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

To exit Pry unconditionally, type

exit-program

Edit from @Nick's comment: Also works:

!!!

How to exit the loop in pry

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

How to step through a loop with pry and view the value of an iterator?

You can type p i and p j to manually inspect them.

That'd make me crazy though, so I'd insert puts i and puts j temporarily.

Don't use for loops with Ruby. Instead we use each or upto, downto or times, depending on our purpose. Also, explicit return statements aren't needed at the end of a method unless you are forcing the code to exit early.

I'd write your code something like:

require 'pry'

def longest_palindrome(s)
max_palindrome_len = 0

s.length.times do |i|
binding.pry
i.upto(s.length) do |j|
binding.pry
substr = s[i..j]
if substr == substr.reverse && substr.length > max_palindrome_len
max_palindrome_len = substr.length
end
end
end

max_palindrome_len
end

longest_palindrome "racer"

How to leave a method in pry

According to the documentation the command you are looking for is f:

finish: Execute until current stack frame returns.

and shortcuts

if defined?(PryDebugger)
Pry.commands.alias_command 'c', 'continue'
Pry.commands.alias_command 's', 'step'
Pry.commands.alias_command 'n', 'next'
Pry.commands.alias_command 'f', 'finish'
end

How to stop binding.pry from executing inside a loop?

You can use !!! or exit-program (exit-p), but it will raise an exception.

How to move to the next line in binding.pry ?

Check out pry-nav, it gives you methods like next and step, which should be what you're looking for.

If you're in regular old Pry you can use exit to go to the next binding.pry or disable-pry to exit Pry entirely.



Related Topics



Leave a reply



Submit