Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set conditional breakpoint with pry-byebug

Tags:

ruby

pry

byebug

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?

like image 666
mbigras Avatar asked Dec 19 '22 10:12

mbigras


2 Answers

I think an easy way is to do it in your code:

require 'pry-byebug'

10.times.with_index do |i|
  binding.pry if i == 5
  puts i
end

Or alternatively if you want to stop at an exception, you can do something like:

require 'pry-byebug'

10.times.with_index do |i|
  puts i rescue binding.pry # if the puts command throws an exception, you'll be taken to pry
end
like image 151
Coolness Avatar answered Jan 09 '23 20:01

Coolness


Remove binding.pry from the inside of the loop and put it just before 10.times:

binding.pry
10.times.with_index do |i|

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:

10.times.with_index do |i|

Instead, this will do almost the same thing but it's simpler:

10.times do |i|

Here's what Ruby is doing:

>> 2.times.with_index.to_a
[
  [0] [
    [0] 0,
    [1] 0
  ],
  [1] [
    [0] 1,
    [1] 1
  ]
]

vs.

>> 2.times.to_a
[
  [0] 0,
  [1] 1
]

The first is passing in arrays of [0,0], [1,1], etc., so for correctness your block parameters need to be something like:

10.times.with_index do |i, j|

The second only passes the current "times" value, resulting in the more simple code.

like image 20
the Tin Man Avatar answered Jan 09 '23 18:01

the Tin Man