Skip to content

Commit 20f1ca2

Browse files
committed
Allow "measure" command to take block
1 parent 7248da8 commit 20f1ca2

File tree

4 files changed

+49
-7
lines changed

4 files changed

+49
-7
lines changed

lib/irb/cmd/measure.rb

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ def initialize(*args)
88
super(*args)
99
end
1010

11-
def execute(type = nil, arg = nil)
11+
def execute(type = nil, arg = nil, &block)
1212
case type
1313
when :off
1414
IRB.conf[:MEASURE] = nil
@@ -22,9 +22,15 @@ def execute(type = nil, arg = nil)
2222
added = IRB.set_measure_callback(type, arg)
2323
puts "#{added[0]} is added." if added
2424
else
25-
IRB.conf[:MEASURE] = true
26-
added = IRB.set_measure_callback(type, arg)
27-
puts "#{added[0]} is added." if added
25+
if block_given?
26+
IRB.conf[:MEASURE] = true
27+
added = IRB.set_measure_callback(&block)
28+
puts "#{added[0]} is added." if added
29+
else
30+
IRB.conf[:MEASURE] = true
31+
added = IRB.set_measure_callback(type, arg)
32+
puts "#{added[0]} is added." if added
33+
end
2834
end
2935
nil
3036
end

lib/irb/cmd/nop.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ module ExtendCommand
1515
class Nop
1616

1717

18-
def self.execute(conf, *opts)
18+
def self.execute(conf, *opts, &block)
1919
command = new(conf)
20-
command.execute(*opts)
20+
command.execute(*opts, &block)
2121
end
2222

2323
def initialize(conf)

lib/irb/init.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ def IRB.init_config(ap_path)
146146
@CONF[:AT_EXIT] = []
147147
end
148148

149-
def IRB.set_measure_callback(type = nil, arg = nil)
149+
def IRB.set_measure_callback(type = nil, arg = nil, &block)
150150
added = nil
151151
if type
152152
type_sym = type.upcase.to_sym
@@ -155,6 +155,8 @@ def IRB.set_measure_callback(type = nil, arg = nil)
155155
end
156156
elsif IRB.conf[:MEASURE_PROC][:CUSTOM]
157157
added = [:CUSTOM, IRB.conf[:MEASURE_PROC][:CUSTOM], arg]
158+
elsif block_given?
159+
added = [:BLOCK, block, arg]
158160
else
159161
added = [:TIME, IRB.conf[:MEASURE_PROC][:TIME], arg]
160162
end

test/irb/test_cmd.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,40 @@ def test_measure_with_custom
276276
assert_match(/\A=> 3\nCUSTOM is added\.\n=> nil\ncustom processing time: .+\n=> 3\n=> nil\n=> 3\n/, out)
277277
end
278278

279+
def test_measure_with_proc
280+
IRB.init_config(nil)
281+
IRB.conf[:PROMPT] = {
282+
DEFAULT: {
283+
PROMPT_I: '> ',
284+
PROMPT_S: '> ',
285+
PROMPT_C: '> ',
286+
PROMPT_N: '> '
287+
}
288+
}
289+
IRB.conf[:VERBOSE] = false
290+
IRB.conf[:PROMPT_MODE] = :DEFAULT
291+
IRB.conf[:MEASURE] = false
292+
input = TestInputMethod.new([
293+
"3\n",
294+
"measure { |context, code, line_no, &block|\n",
295+
" result = block.()\n",
296+
" puts 'aaa' if IRB.conf[:MEASURE]\n",
297+
"}\n",
298+
"3\n",
299+
"measure :off\n",
300+
"3\n",
301+
])
302+
c = Class.new(Object)
303+
irb = IRB::Irb.new(IRB::WorkSpace.new(c.new), input)
304+
irb.context.return_format = "=> %s\n"
305+
out, err = capture_output do
306+
irb.eval_input
307+
end
308+
assert_empty err
309+
assert_match(/\A=> 3\nBLOCK is added\.\n=> nil\naaa\n=> 3\n=> nil\n=> 3\n/, out)
310+
assert_empty(c.class_variables)
311+
end
312+
279313
def test_irb_source
280314
IRB.init_config(nil)
281315
File.write("#{@tmpdir}/a.rb", "a = 'hi'\n")

0 commit comments

Comments
 (0)