Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More human friendly way to call debugger, i.e. 'debug' or 'debugger' #378

Closed
DanielJackson-Oslo opened this issue Nov 9, 2021 · 4 comments

Comments

@DanielJackson-Oslo
Copy link

Currently we use binding.break to run the debug gem.

To me, it would make sense to have an alias for binding.break named debug so I don't have to know which Ruby core module break is part of to remember how to debug.

Example code:

# Un-aliased use, still valid: 

some_code_that_doesnt_work
binding.break

# Aliased use

some_code_that_doesnt_work
debug

I think this better satisfies Ruby's goal of least surprise.

@st0012
Copy link
Member

st0012 commented Nov 9, 2021

@DanielJackson-Oslo debugger is actually supported 😄

@ko1 ko1 closed this as completed Nov 9, 2021
@DanielJackson-Oslo
Copy link
Author

DanielJackson-Oslo commented Nov 10, 2021

@DanielJackson-Oslo debugger is actually supported 😄

Fantastic! And slow of me to not test if it was there already.

Should the readme then be changed to say debugger is an option, or perhaps even the default?

Currently it's undocumented in the readme - but to me it feels more natural to have it as the default, so that the section on how to use would read as follows:


Modify source code with debugger

If you can modify the source code, you can use the debugger by adding require 'debug' line at the top of your program and putting debugger method into lines where you want to stop as breakpoints. After that, you run the program as usual and you will enter the debug console at breakpoints you inserted.

The debugger command is an alias for binding.break, and is interchangeable with binding.break (like binding.pry and binding.irb) or the short versoin binding.b if that feels natural.

The following example shows a demonstration of debugger.

$ cat target.rb                        # Sample program
require 'debug'

a = 1
b = 2
debugger                               # Program will stop here
c = 3
d = 4
debugger                               # Program will stop here
p [a, b, c, d]

$ ruby target.rb                       # Run the program normally.
DEBUGGER: Session start (pid: 7604)
[1, 10] in target.rb
      1| require 'debug'
      2|
      3| a = 1
      4| b = 2
=>    5| debugger                      # Now you can see it stops at this line
      6| c = 3
      7| d = 4
      8| binding.break
      9| p [a, b, c, d]
     10|
=>#0    <main> at target.rb:5

(rdbg) info locals                     # You can show local variables
=>#0    <main> at target.rb:5
%self => main
a => 1
b => 2
c => nil
d => nil

(rdbg) continue                        # Continue the execution
[3, 11] in target.rb
      3| a = 1
      4| b = 2
      5| debugger
      6| c = 3
      7| d = 4
=>    8| debugger                     # Again the program stops at here
      9| p [a, b, c, d]
     10|
     11| __END__
=>#0    <main> at target.rb:8

(rdbg) info locals                     # And you can see the updated local variables
=>#0    <main> at target.rb:8
%self => main
a => 1
b => 2
c => 3
d => 4

(rdbg) continue
[1, 2, 3, 4]

@ko1
Copy link
Collaborator

ko1 commented Nov 10, 2021

At least it should be documented. Thanks.

@st0012
Copy link
Member

st0012 commented Nov 30, 2021

I've added #418 to mention this alias in the readme.

@ko1 ko1 closed this as completed Dec 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants