diff --git a/lib/debug/thread_client.rb b/lib/debug/thread_client.rb index 0e1fa42a1..346076bf2 100644 --- a/lib/debug/thread_client.rb +++ b/lib/debug/thread_client.rb @@ -443,7 +443,9 @@ def frame_eval src, re_raise: false, binding_location: false b.local_variable_set(name, var) if /\%/ !~ name end + b.local_variable_set(:_, @last_result) result = frame_eval_core(src, b, binding_location: binding_location) + @last_result = result @success_last_eval = true result diff --git a/test/console/debugger_local_test.rb b/test/console/debugger_local_test.rb index f32f43d0c..cfd18f2b0 100644 --- a/test/console/debugger_local_test.rb +++ b/test/console/debugger_local_test.rb @@ -19,6 +19,18 @@ def test_locals_added_in_locals_are_accessible_between_evaluations type "c" end end + + def test_evaluated_result_is_set_as_underscore_local + debug_code(program) do + type "_" + assert_line_text(/nil/) + type "3 * 3" + type "foo = _" + type "9 == foo" + assert_line_text(/true/) + type "c" + end + end end class RaisedTest < ConsoleTestCase diff --git a/test/console/irb_test.rb b/test/console/irb_test.rb index 746f9acdb..de4290050 100644 --- a/test/console/irb_test.rb +++ b/test/console/irb_test.rb @@ -84,6 +84,20 @@ def test_irb_console_config_activates_irb ENV["RUBY_DEBUG_IRB_CONSOLE"] = nil end + def test_irb_console_evaluated_result_is_set_as_underscore_local + debug_code(program, remote: false) do + type 'irb' + type "_" + assert_line_text(/nil/) + type "3 * 3" + assert_raw_line_text 'irb:rdbg(main):003> 3 * 3' + type "foo = _" + type "9 == foo" + assert_line_text(/true/) + type "c" + end + end + private # assert_line_text ignores the prompt line, so we can't use it to assert the prompt transition