From 39b38ebdfbdf3821efefa051634bfb796b8926fd Mon Sep 17 00:00:00 2001 From: Gabriel Lidenor Date: Sun, 6 Sep 2026 21:23:42 -0500 Subject: [PATCH] fix: measure does not work with exceptions The reason it was failing it was because when an exception ocurred the code stop in result = block.() preventing the lines to display time to be executed. The begin, ensure forces the code to execute the other lines after the excception --- lib/irb/init.rb | 9 ++++++--- test/irb/test_init.rb | 11 +++++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/lib/irb/init.rb b/lib/irb/init.rb index f933f5cec..7a16d922a 100644 --- a/lib/irb/init.rb +++ b/lib/irb/init.rb @@ -148,9 +148,12 @@ def IRB.init_config(ap_path) @CONF[:MEASURE_PROC] = {} @CONF[:MEASURE_PROC][:TIME] = proc { |context, code, line_no, &block| time = Time.now - result = block.() - now = Time.now - puts 'processing time: %fs' % (now - time) if IRB.conf[:MEASURE] + begin + result = block.() + ensure + now = Time.now + puts 'processing time: %fs' % (now - time) if IRB.conf[:MEASURE] + end result } # arg can be either a symbol for the mode (:cpu, :wall, ..) or a hash for diff --git a/test/irb/test_init.rb b/test/irb/test_init.rb index b68681e38..1e2e63d0a 100644 --- a/test/irb/test_init.rb +++ b/test/irb/test_init.rb @@ -290,6 +290,17 @@ def test_nobanner assert_equal(false, IRB.conf[:SHOW_BANNER]) end + def test_measure_when_exception_occurs + out, err = execute_lines( + "measure\n", + "raise 'boom'\n" + ) + + assert_empty err + assert_match(/processing time: \d+\.\d+s/, out) + assert_match(/boom \(RuntimeError\)/, out) + end + private def with_argv(argv)