diff options
-rw-r--r-- | lib/irb.rb | 20 | ||||
-rw-r--r-- | lib/irb/context.rb | 24 | ||||
-rw-r--r-- | test/irb/test_cmd.rb | 28 |
3 files changed, 53 insertions, 19 deletions
diff --git a/lib/irb.rb b/lib/irb.rb index ed79160b24..64c716fd4c 100644 --- a/lib/irb.rb +++ b/lib/irb.rb @@ -558,26 +558,10 @@ module IRB @scanner.each_top_level_statement do |line, line_no| signal_status(:IN_EVAL) do begin - if IRB.conf[:MEASURE] && IRB.conf[:MEASURE_CALLBACKS].empty? - IRB.set_measure_callback - end # Assignment expression check should be done before evaluate_line to handle code like `a /2#/ if false; a = 1` is_assignment = assignment_expression?(line) - if IRB.conf[:MEASURE] && !IRB.conf[:MEASURE_CALLBACKS].empty? - result = nil - last_proc = proc{ result = evaluate_line(line, line_no) } - IRB.conf[:MEASURE_CALLBACKS].inject(last_proc) { |chain, item| - _name, callback, arg = item - proc { - callback.(@context, line, line_no, arg) do - chain.call - end - } - }.call - @context.set_last_value(result) - else - evaluate_line(line, line_no) - end + evaluate_line(line, line_no) + if @context.echo? if is_assignment if @context.echo_on_assignment? diff --git a/lib/irb/context.rb b/lib/irb/context.rb index 58964aa832..6f209b596a 100644 --- a/lib/irb/context.rb +++ b/lib/irb/context.rb @@ -475,7 +475,29 @@ module IRB def evaluate(line, line_no) # :nodoc: @line_no = line_no - set_last_value(@workspace.evaluate(line, irb_path, line_no)) + result = nil + + if IRB.conf[:MEASURE] && IRB.conf[:MEASURE_CALLBACKS].empty? + IRB.set_measure_callback + end + + if IRB.conf[:MEASURE] && !IRB.conf[:MEASURE_CALLBACKS].empty? + last_proc = proc do + result = @workspace.evaluate(line, irb_path, line_no) + end + IRB.conf[:MEASURE_CALLBACKS].inject(last_proc) do |chain, item| + _name, callback, arg = item + proc do + callback.(self, line, line_no, arg) do + chain.call + end + end + end.call + else + result = @workspace.evaluate(line, irb_path, line_no) + end + + set_last_value(result) end def inspect_last_value # :nodoc: diff --git a/test/irb/test_cmd.rb b/test/irb/test_cmd.rb index c8c163c82d..71df60db14 100644 --- a/test/irb/test_cmd.rb +++ b/test/irb/test_cmd.rb @@ -252,6 +252,34 @@ module TestIRB assert_empty(c.class_variables) end + def test_measure_keeps_previous_value + conf = { + PROMPT: { + DEFAULT: { + PROMPT_I: '> ', + PROMPT_S: '> ', + PROMPT_C: '> ', + PROMPT_N: '> ' + } + }, + PROMPT_MODE: :DEFAULT, + MEASURE: false + } + + c = Class.new(Object) + out, err = execute_lines( + "measure\n", + "3\n", + "_\n", + conf: conf, + main: c + ) + + assert_empty err + assert_match(/\ATIME is added\.\n=> nil\nprocessing time: .+\n=> 3\nprocessing time: .+\n=> 3/, out) + assert_empty(c.class_variables) + end + def test_measure_enabled_by_rc conf = { PROMPT: { |