Skip to content

Preserve an existing _ local when creating a WorkSpace - #1266

Open
joe-sharp wants to merge 1 commit into
ruby:masterfrom
joe-sharp:preserve-existing-underscore
Open

joe-sharp wants to merge 1 commit into
ruby:masterfrom
joe-sharp:preserve-existing-underscore

Conversation

@joe-sharp

Copy link
Copy Markdown

Fixes #891. Works together with ruby/debug#1188.

WorkSpace#initialize always called @binding.local_variable_set(:_, nil). If the binding already has a _ local, Binding#local_variable_set overwrites the program's variable, so _ = :mine; binding.irb shows nil. The debugger's IRB console mode creates a new WorkSpace at every prompt, so the value is reset each time.

This change only sets _ to nil when the binding doesn't already define it.

Tests:

  • test_workspace_keeps_existing_underscore (new): fails before the fix, passes after.
  • test_workspace_defines_underscore_as_nil_when_absent (new): existing behavior, passes.
  • test_irb_console_mode_keeps_program_defined_underscore (new, debugger integration): fails before the fix, passes after.
  • Full suite: one failure, test_context_mode_ruby_box, which also fails without this change on Ruby 4.0.9 here.

Known separate question, not changed here: Context#set_last_value still writes _ after each evaluated expression, so a program's _ becomes the last result after the first expression in plain binding.irb. I left that as is.

🤖 Generated with Claude Code

WorkSpace#initialize unconditionally ran `local_variable_set(:_, nil)`.
When the binding already had a `_` local, Binding#local_variable_set
overwrote the program's variable, so `_ = :mine; binding.irb` showed
nil. The debugger's IRB console mode creates a new WorkSpace at every
prompt, so the value was reset each time.

Only set `_` to nil when the binding does not already define it.

Refs ruby#891

Co-Authored-By: Claude Haiku 5.5 <[email protected]>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

After entering debug, _ (underscore) stops working

1 participant