Given the following code:
require 'pry-byebug'
10.times.with_index do |i|
binding.pry
puts i
end
I'd like to "loop until i == 5
then break" while inside pry-byebug
. From running it help break
it seems like you can identify breakpoints by "breakpoint #" or "line #". It also seems like you can use conditions, however I'm not getting it to work:
Trying to set breakpoint on line #:
$ ruby foo.rb
From: /Users/max/Dropbox/work/tmp/pry_debug/foo.rb @ line 5 :
1: require 'pry-byebug'
2:
3: 10.times.with_index do |i|
4: binding.pry
=> 5: puts i
6: end
[1] pry(main)> break foo.rb:5 if i == 5
Breakpoint 1: /Users/max/Dropbox/work/tmp/pry_debug/foo.rb @ 5 (Enabled) Condition: i == 5
2:
3: 10.times.with_index do |i|
4: binding.pry
=> 5: puts i
6: end
[2] pry(main)> continue
0
From: /Users/max/Dropbox/work/tmp/pry_debug/foo.rb @ line 5 :
1: require 'pry-byebug'
2:
3: 10.times.with_index do |i|
4: binding.pry
=> 5: puts i
6: end
[2] pry(main)> i
=> 1
First setting breakpoint then putting condition on break point:
➜ ~/D/w/t/pry_debug ruby foo.rb
From: /Users/max/Dropbox/work/tmp/pry_debug/foo.rb @ line 5 :
1: require 'pry-byebug'
2:
3: 10.times.with_index do |i|
4: binding.pry
=> 5: puts i
6: end
[1] pry(main)> break foo.rb:15
Breakpoint 1: /Users/max/Dropbox/work/tmp/pry_debug/foo.rb @ 15 (Enabled)
[2] pry(main)> break --condition 1 i == 5
[3] pry(main)> c
0
From: /Users/max/Dropbox/work/tmp/pry_debug/foo.rb @ line 5 :
1: require 'pry-byebug'
2:
3: 10.times.with_index do |i|
4: binding.pry
=> 5: puts i
6: end
[3] pry(main)> i
=> 1
As you can see, in both cases pry-byebug
doesn't respect the condition because it stops too soon. How do I get it to work?
Remove
binding.pry
from the inside of the loop and put it just before10.times
:Then run the code. When it hits the breakpoint then set a new breakpoint with the conditional you want and
continue
.Back to your actual code. Don't do this:
Instead, this will do almost the same thing but it's simpler:
Here's what Ruby is doing:
vs.
The first is passing in arrays of
[0,0]
,[1,1]
, etc., so for correctness your block parameters need to be something like:The second only passes the current "times" value, resulting in the more simple code.