Byebug breakpoint setting and continuation trouble

I am trying to learn Byebug and am following the ( and I am getting stuck with setting a breakpoint and continuing after the breakpoint is set. After I set the breakpoint for line 5 and run continue, Byebug quits the session. Can someone see what I am doing wrong?

    $byebug hanoi.rb 

        [1, 10] in /Users/jackburum/ruby/byebug-testing/hanoi.rb
            1: #
            2: #
            3: #
        =>  4: def hanoi(n, a, b, c)
            5:   hanoi(n - 1, a, c, b) if n - 1 > 0
            7:   puts "Move disk #{a} to #{b}"
            9:   hanoi(n - 1, c, b, a) if n - 1 > 0
           10: end
        (byebug) break 5
        Successfully created breakpoint with id 1
        (byebug) continue
 $byebug hanoi.rb 

If I run the next command instead of continue, the same issue occurs...

[1, 10] in /Users/jackburum/ruby/byebug-testing/triangle.rb
=>  1: def triangle(n)
    2:   tri = 0
    4:   0.upto(n) { |i| tri += i }
    6:   tri
    7: end
    9: # if __FILE__ == $triangle.rb
   10: #   t = triangle(3)
(byebug) break 2
Successfully created breakpoint with id 1
(byebug) next


are you meaning to use next here? continue is the same control+d, and will continue to run your code until either another breakpoint is reached or the code ends.

I'm figuring that when you type continue, the rest of your script runs and the program exits because there isn't another breakpoint.

By the way, it might be worth trying the pry gem instead of byebug. Unlike byebug, copy-pasting multi-line code works in pry and there are a number of other useful features such as source-code browsing and syntax highlighting. Although it doesn't come with 'next' or 'continue' out the box, you can install the pry-byebug gem to get these.


From the additional info you've given, this seems to actually be the expected behavior here. Here's what you need to understand about breakpoints:

They will not follow a downward scope change. For example, say I've written a method like this:

def my_method
  puts "my method started"
  5.times { "do something in a loop" }
puts "my method is done"
puts "the program will end now"

When you run the code with ruby myfile.rb, the code gets to line 6 before pausing on the breakpoint. Since the breakpoint is inside the method body, it won't be activated until the method is actually called.

What would happen if you typed next? Would you have to type it 5 times to get through the 5.times {} loop? The answer is no, and this is because there is a downward scope change into the iterator. In Javascript this "scope change" is more clear because you use the function keyword to show an anonymous function (and therefore a scope change). But in Ruby, blocks are actually syntactic sugar for Procs, which are anonymous functions and therefore introduce a scope change. I'm not sure of the exact reason, but moving into a method body also entails a downward scope change.

So when you press next, what actually happens is that it moves to the puts "my method is done" call. Although the byebug keyword is inside the method, to return to the scope of the method caller is an 'upward' scope change and is expected. Typing next again would move to the puts "my program will end now" line. Typing it again would get to the end of the code and exit the program.

If you needed to pause at each iteration in the loop, you could put the breakpoint inside the loop block i.e. 5.times { byebug }

To bring it back to your example - when you call your script with the byebug executable (i.e. byebug myprogram.rb), the initial scope is top-level. Typing next will skip over any functions or loops. I don't see this being practical for anything other than very small programs, so it's better to place the byebug keyword in your code and call the program with the ruby executable instead, i.e. ruby myprogram.rb

It's a small detail, but breakpoints are ignored when they are the last line in a program. To prevent this, put anything after the breakpoint, even something as simple as exit.

By the way, all this scope stuff applies to pry too as far as I'm aware.

Need Your Help

payone integration with creditcardcheck

javascript php payment-gateway payment payone

I have read almost everything there is on the Internet (almost no examples) and studied the (very limited and confusing) documentation.

Is it possible to color a strikethrough line differ from the main text color in the .docx file via editing its data files?

xml ms-word openxml docx styling

Any .docx file is an archive with data files in it. The main content of the any .docx file is stored in the word\document.xml data file inside the .docx file. Strikethrough lines are represented as...