ruby-changes:65158
From: aycabta <ko1@a...>
Date: Sat, 6 Feb 2021 20:45:26 +0900 (JST)
Subject: [ruby-changes:65158] 5704b5fe5e (master): [ruby/irb] Allow "measure" command to take block
https://git.ruby-lang.org/ruby.git/commit/?id=5704b5fe5e From 5704b5fe5e42bd5b1f42a27368cd5d52dd5a9060 Mon Sep 17 00:00:00 2001 From: aycabta <aycabta@g...> Date: Sat, 6 Feb 2021 20:23:51 +0900 Subject: [ruby/irb] Allow "measure" command to take block https://github.com/ruby/irb/commit/20f1ca23e9 --- lib/irb/cmd/measure.rb | 14 ++++++++++---- lib/irb/cmd/nop.rb | 4 ++-- lib/irb/init.rb | 4 +++- test/irb/test_cmd.rb | 34 ++++++++++++++++++++++++++++++++++ 4 files changed, 49 insertions(+), 7 deletions(-) diff --git a/lib/irb/cmd/measure.rb b/lib/irb/cmd/measure.rb index 5e0bef6..58eaec2 100644 --- a/lib/irb/cmd/measure.rb +++ b/lib/irb/cmd/measure.rb @@ -8,7 +8,7 @@ module IRB https://github.com/ruby/ruby/blob/trunk/lib/irb/cmd/measure.rb#L8 super(*args) end - def execute(type = nil, arg = nil) + def execute(type = nil, arg = nil, &block) case type when :off IRB.conf[:MEASURE] = nil @@ -22,9 +22,15 @@ module IRB https://github.com/ruby/ruby/blob/trunk/lib/irb/cmd/measure.rb#L22 added = IRB.set_measure_callback(type, arg) puts "#{added[0]} is added." if added else - IRB.conf[:MEASURE] = true - added = IRB.set_measure_callback(type, arg) - puts "#{added[0]} is added." if added + if block_given? + IRB.conf[:MEASURE] = true + added = IRB.set_measure_callback(&block) + puts "#{added[0]} is added." if added + else + IRB.conf[:MEASURE] = true + added = IRB.set_measure_callback(type, arg) + puts "#{added[0]} is added." if added + end end nil end diff --git a/lib/irb/cmd/nop.rb b/lib/irb/cmd/nop.rb index 9cf4337..fa3c011 100644 --- a/lib/irb/cmd/nop.rb +++ b/lib/irb/cmd/nop.rb @@ -15,9 +15,9 @@ module IRB https://github.com/ruby/ruby/blob/trunk/lib/irb/cmd/nop.rb#L15 class Nop - def self.execute(conf, *opts) + def self.execute(conf, *opts, &block) command = new(conf) - command.execute(*opts) + command.execute(*opts, &block) end def initialize(conf) diff --git a/lib/irb/init.rb b/lib/irb/init.rb index 8428a42..cd57012 100644 --- a/lib/irb/init.rb +++ b/lib/irb/init.rb @@ -146,7 +146,7 @@ module IRB # :nodoc: https://github.com/ruby/ruby/blob/trunk/lib/irb/init.rb#L146 @CONF[:AT_EXIT] = [] end - def IRB.set_measure_callback(type = nil, arg = nil) + def IRB.set_measure_callback(type = nil, arg = nil, &block) added = nil if type type_sym = type.upcase.to_sym @@ -155,6 +155,8 @@ module IRB # :nodoc: https://github.com/ruby/ruby/blob/trunk/lib/irb/init.rb#L155 end elsif IRB.conf[:MEASURE_PROC][:CUSTOM] added = [:CUSTOM, IRB.conf[:MEASURE_PROC][:CUSTOM], arg] + elsif block_given? + added = [:BLOCK, block, arg] else added = [:TIME, IRB.conf[:MEASURE_PROC][:TIME], arg] end diff --git a/test/irb/test_cmd.rb b/test/irb/test_cmd.rb index 106e4d2..045ce0f 100644 --- a/test/irb/test_cmd.rb +++ b/test/irb/test_cmd.rb @@ -276,6 +276,40 @@ module TestIRB https://github.com/ruby/ruby/blob/trunk/test/irb/test_cmd.rb#L276 assert_match(/\A=> 3\nCUSTOM is added\.\n=> nil\ncustom processing time: .+\n=> 3\n=> nil\n=> 3\n/, out) end + def test_measure_with_proc + IRB.init_config(nil) + IRB.conf[:PROMPT] = { + DEFAULT: { + PROMPT_I: '> ', + PROMPT_S: '> ', + PROMPT_C: '> ', + PROMPT_N: '> ' + } + } + IRB.conf[:VERBOSE] = false + IRB.conf[:PROMPT_MODE] = :DEFAULT + IRB.conf[:MEASURE] = false + input = TestInputMethod.new([ + "3\n", + "measure { |context, code, line_no, &block|\n", + " result = block.()\n", + " puts 'aaa' if IRB.conf[:MEASURE]\n", + "}\n", + "3\n", + "measure :off\n", + "3\n", + ]) + c = Class.new(Object) + irb = IRB::Irb.new(IRB::WorkSpace.new(c.new), input) + irb.context.return_format = "=> %s\n" + out, err = capture_output do + irb.eval_input + end + assert_empty err + assert_match(/\A=> 3\nBLOCK is added\.\n=> nil\naaa\n=> 3\n=> nil\n=> 3\n/, out) + assert_empty(c.class_variables) + end + def test_irb_source IRB.init_config(nil) File.write("#{@tmpdir}/a.rb", "a = 'hi'\n") -- cgit v1.1 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/