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
How to Run Ruby on Rails with Ruby 1.9X
Is Every Relavant Calculation Performed Every Time the Page Is Loaded
Message Queues in Ruby on Rails
Extract Links (Urls), with Nokogiri in Ruby, from a Href HTML Tags
Where to Place/Access Config File in Gem
How to Redefine a Ruby Constant Without Warning
Rails Rbenv: Rails: Command Not Found
Is 'Yield Self' the Same as Instance_Eval
Find Most Common String in an Array
Ruby Koans: Explicit Scoping on a Class Definition Part 2
Invalid Multibyte Char (Utf-8) Error, Ruby
How to Fix a Deadlock Caused by Open
How to List All Versions of a Gem Available at a Remote Site
How to List the Currently Available Objects in the Current Scope in Ruby
Imagemagick 7 with Rmagick 2.16 on MACos Sierra Can't Find Magickwand.H
Relative Path to Your Project Directory