ruby / debug

Debugging functionality for Ruby
BSD 2-Clause "Simplified" License
1.12k stars 124 forks source link

Feat/Bug: Set the value of `_` (underscore) to the value of the last ruby expression #1069

Open joe-sharp opened 8 months ago

joe-sharp commented 8 months ago

Your proposal

Pry and IRB have a feature in which _ is always set the the value of the last expression. While I realize it may not make sense to do this with debug commands, it would be nice if the feature was there at least for Ruby expressions. The current behavior seems to be setting _ to the last error, but seemingly only for debug commands like ls. This is kind of a bug in the irb_console since IRB has this feature normally but it stops working after entering debug mode.

Additional context

Shell session:

```rb irb(#):001> 3+3 => 6 irb(#):002> puts _ 6 => nil irb(#):003> 4+4 => 8 irb(#):004> info (rdbg:irb) info %self = # @foo = "bar" irb:rdbg(#):002> puts _ nil irb:rdbg(#):003> 4+4 8 irb:rdbg(#):004> puts _ nil irb:rdbg(#):005> 4+4 8 irb:rdbg(#):006> info %self = # _ = nil @foo = "bar" irb:rdbg(#):007> ls Bar Traceback (most recent call last): 24: from /Users/joesharp/.asdf/installs/ruby/2.7.8/lib/ruby/gems/2.7.0/gems/debug-1.9.1/lib/debug/session.rb:212:in `block in activate' 23: from /Users/joesharp/.asdf/installs/ruby/2.7.8/lib/ruby/gems/2.7.0/gems/debug-1.9.1/lib/debug/session.rb:254:in `session_server_main' 22: from /Users/joesharp/.asdf/installs/ruby/2.7.8/lib/ruby/gems/2.7.0/gems/debug-1.9.1/lib/debug/session.rb:365:in `process_event' 21: from /Users/joesharp/.asdf/installs/ruby/2.7.8/lib/ruby/gems/2.7.0/gems/debug-1.9.1/lib/debug/session.rb:403:in `wait_command_loop' 20: from /Users/joesharp/.asdf/installs/ruby/2.7.8/lib/ruby/gems/2.7.0/gems/debug-1.9.1/lib/debug/session.rb:403:in `loop' 19: from /Users/joesharp/.asdf/installs/ruby/2.7.8/lib/ruby/gems/2.7.0/gems/debug-1.9.1/lib/debug/session.rb:404:in `block in wait_command_loop' 18: from /Users/joesharp/.asdf/installs/ruby/2.7.8/lib/ruby/gems/2.7.0/gems/debug-1.9.1/lib/debug/session.rb:444:in `wait_command' 15: from /Users/joesharp/.asdf/installs/ruby/2.7.8/lib/ruby/gems/2.7.0/gems/debug-1.9.1/lib/debug/session.rb:1981:in `intercept_trap_sigint' 4: from /Users/joesharp/.asdf/installs/ruby/2.7.8/lib/ruby/gems/2.7.0/gems/debug-1.9.1/lib/debug/irb_integration.rb:9:in `evaluate' (irb):7:in `set_foo': uninitialized constant Foobar::Bar (NameError)` irb:rdbg(#):008> puts _ uninitialized constant Foobar::Bar nil irb:rdbg(#):009> ls Bar Traceback (most recent call last): 24: from /Users/joesharp/.asdf/installs/ruby/2.7.8/lib/ruby/gems/2.7.0/gems/debug-1.9.1/lib/debug/session.rb:212:in `block in activate' 23: from /Users/joesharp/.asdf/installs/ruby/2.7.8/lib/ruby/gems/2.7.0/gems/debug-1.9.1/lib/debug/session.rb:254:in `session_server_main' 22: from /Users/joesharp/.asdf/installs/ruby/2.7.8/lib/ruby/gems/2.7.0/gems/debug-1.9.1/lib/debug/session.rb:365:in `process_event' 21: from /Users/joesharp/.asdf/installs/ruby/2.7.8/lib/ruby/gems/2.7.0/gems/debug-1.9.1/lib/debug/session.rb:403:in `wait_command_loop' 20: from /Users/joesharp/.asdf/installs/ruby/2.7.8/lib/ruby/gems/2.7.0/gems/debug-1.9.1/lib/debug/session.rb:403:in `loop' 19: from /Users/joesharp/.asdf/installs/ruby/2.7.8/lib/ruby/gems/2.7.0/gems/debug-1.9.1/lib/debug/session.rb:404:in `block in wait_command_loop' 18: from /Users/joesharp/.asdf/installs/ruby/2.7.8/lib/ruby/gems/2.7.0/gems/debug-1.9.1/lib/debug/session.rb:444:in `wait_command' 15: from /Users/joesharp/.asdf/installs/ruby/2.7.8/lib/ruby/gems/2.7.0/gems/debug-1.9.1/lib/debug/session.rb:1981:in `intercept_trap_sigint' 4: from /Users/joesharp/.asdf/installs/ruby/2.7.8/lib/ruby/gems/2.7.0/gems/debug-1.9.1/lib/debug/irb_integration.rb:9:in `evaluate' (irb):9:in `set_foo': uninitialized constant Foobar::Bar (NameError)` irb:rdbg(#):010> info %self = # _ = # @foo = "bar" irb:rdbg(#):011> Bar.methods eval error: uninitialized constant Foobar::Bar (rdbg)/scratch.rb:1:in `set_foo'` nil irb:rdbg(#):012> info %self = # _ = nil @foo = "bar" irb:rdbg(#):013> Bar.methods eval error: uninitialized constant Foobar::Bar (rdbg)/scratch.rb:1:in `set_foo'` nil irb:rdbg(#):014> puts _ nil ```

joe-sharp commented 8 months ago

Please let me know if the bug portion of this (_ stops working in IRB when entering debug mode) should be filed as a separate issue. Thanks!

joe-sharp commented 8 months ago

Additionally, please note in the "Session Variables" section of https://docs.ruby-lang.org/en/2.2.0/IRB.html#module-IRB-label-Commands that IRB also supports __ (double underscore) and __[line_no] when configured to do so. It always supports the single underscore for the last value though.

st0012 commented 8 months ago

Please let me know if the bug portion of this (_ stops working in IRB when entering debug mode) should be filed as a separate issue. Thanks!

Yes please open a separate issue in IRB, thank you!