Skip to content

Commit 73b7693

Browse files
committed
Deprecate measure :on and navigate to use measure on
1 parent 55548fd commit 73b7693

4 files changed

Lines changed: 66 additions & 41 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ Debugging
152152
153153
Misc
154154
edit Open a file or source location.
155-
measure `measure` enables the mode to measure processing time. `measure :off` disables it.
155+
measure `measure` enables the mode to measure processing time. `measure off` disables it.
156156
157157
Context
158158
show_doc Look up documentation with RI.

lib/irb/command/measure.rb

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,51 @@ module IRB
33

44
module Command
55
class Measure < Base
6-
include RubyArgsExtractor
7-
86
category "Misc"
9-
description "`measure` enables the mode to measure processing time. `measure :off` disables it."
10-
11-
def initialize(*args)
12-
super(*args)
13-
end
7+
description "`measure` enables the mode to measure processing time. `measure off` disables it."
148

159
def execute(arg)
1610
if arg&.match?(/^do$|^do[^\w]|^\{/)
1711
warn 'Configure IRB.conf[:MEASURE_PROC] to add custom measure methods.'
1812
return
1913
end
20-
args, kwargs = ruby_args(arg)
21-
execute_internal(*args, **kwargs)
14+
15+
if arg.empty?
16+
execute_internal(nil, nil)
17+
elsif arg.start_with? ':'
18+
# Legacy style `measure :stackprof`, `measure :off, :time`
19+
type, arg_val = arg.split(/,\s*/, 2).map { |v| v.sub(/\A:/, '') }
20+
warn "`measure #{arg}` is deprecated. Please use `measure #{[type, arg_val].compact.join(' ')}` instead."
21+
execute_internal(type.to_sym, arg_val)
22+
else
23+
type, arg_val = arg.split(/\s+/, 2)
24+
execute_internal(type.to_sym, arg_val)
25+
end
2226
end
2327

24-
def execute_internal(type = nil, arg = nil)
28+
def execute_internal(type, arg)
2529
# Please check IRB.init_config in lib/irb/init.rb that sets
2630
# IRB.conf[:MEASURE_PROC] to register default "measure" methods,
27-
# "measure :time" (abbreviated as "measure") and "measure :stackprof".
31+
# "measure time" (abbreviated as "measure") and "measure stackprof".
2832

2933
case type
3034
when :off
31-
IRB.unset_measure_callback(arg)
35+
IRB.unset_measure_callback(arg&.to_sym)
3236
when :list
3337
IRB.conf[:MEASURE_CALLBACKS].each do |type_name, _, arg_val|
3438
puts "- #{type_name}" + (arg_val ? "(#{arg_val.inspect})" : '')
3539
end
36-
when :on
37-
added = IRB.set_measure_callback(arg)
38-
puts "#{added[0]} is added." if added
3940
else
40-
added = IRB.set_measure_callback(type, arg)
41-
puts "#{added[0]} is added." if added
41+
type, arg = arg&.to_sym, nil if type == :on
42+
43+
measure_methods = IRB.conf[:MEASURE_PROC].keys.map(&:downcase)
44+
if type && !measure_methods.include?(type)
45+
puts "Measure method `#{type}` not found."
46+
puts "Available measure methods: %w[#{measure_methods.join(' ')}]."
47+
else
48+
added = IRB.set_measure_callback(type&.to_sym, arg)
49+
puts "#{added[0]} is added." if added
50+
end
4251
end
4352
nil
4453
end

lib/irb/init.rb

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -204,19 +204,9 @@ def IRB.set_measure_callback(type = nil, arg = nil, &block)
204204
added = [type_sym, IRB.conf[:MEASURE_PROC][type_sym], arg]
205205
end
206206
elsif IRB.conf[:MEASURE_PROC][:CUSTOM]
207-
added = [:CUSTOM, IRB.conf[:MEASURE_PROC][:CUSTOM], arg]
208-
elsif block_given?
209-
added = [:BLOCK, block, arg]
210-
found = IRB.conf[:MEASURE_CALLBACKS].find{ |m| m[0] == added[0] && m[2] == added[2] }
211-
if found
212-
found[1] = block
213-
return added
214-
else
215-
IRB.conf[:MEASURE_CALLBACKS] << added
216-
return added
217-
end
207+
added = [:CUSTOM, IRB.conf[:MEASURE_PROC][:CUSTOM], nil]
218208
else
219-
added = [:TIME, IRB.conf[:MEASURE_PROC][:TIME], arg]
209+
added = [:TIME, IRB.conf[:MEASURE_PROC][:TIME], nil]
220210
end
221211
if added
222212
IRB.conf[:MEASURE] = true

test/irb/test_command.rb

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -232,11 +232,11 @@ def test_measure
232232
out, err = execute_lines(
233233
"measure\n",
234234
"3\n",
235-
"measure :off\n",
235+
"measure off\n",
236236
"3\n",
237-
"measure :on\n",
237+
"measure on\n",
238238
"3\n",
239-
"measure :off\n",
239+
"measure off\n",
240240
"3\n",
241241
conf: conf,
242242
main: c
@@ -289,7 +289,7 @@ def test_measure_enabled_by_rc
289289

290290
out, err = execute_lines(
291291
"3\n",
292-
"measure :off\n",
292+
"measure off\n",
293293
"3\n",
294294
conf: conf,
295295
)
@@ -314,7 +314,7 @@ def test_measure_enabled_by_rc_with_custom
314314

315315
out, err = execute_lines(
316316
"3\n",
317-
"measure :off\n",
317+
"measure off\n",
318318
"3\n",
319319
conf: conf,
320320
)
@@ -339,7 +339,7 @@ def test_measure_with_custom
339339
"3\n",
340340
"measure\n",
341341
"3\n",
342-
"measure :off\n",
342+
"measure off\n",
343343
"3\n",
344344
conf: conf
345345
)
@@ -365,19 +365,19 @@ def test_measure_toggle
365365
}
366366
}
367367
out, err = execute_lines(
368-
"measure :foo, :arg\n",
368+
"measure foo arg\n",
369369
"1\n",
370-
"measure :on, :bar\n",
370+
"measure on bar\n",
371371
"2\n",
372-
"measure :off, :foo\n",
372+
"measure off foo\n",
373373
"3\n",
374-
"measure :off, :bar\n",
374+
"measure off bar\n",
375375
"4\n",
376376
conf: conf
377377
)
378378

379379
assert_empty err
380-
assert_match(/\AFOO is added\.\nfoo\(:arg\)\n=> 1\nBAR is added\.\nbar\(nil\)\nfoo\(:arg\)\n=> 2\nbar\(nil\)\n=> 3\n=> 4\n/, out)
380+
assert_match(/\AFOO is added\.\nfoo\("arg"\)\n=> 1\nBAR is added\.\nbar\(nil\)\nfoo\("arg"\)\n=> 2\nbar\(nil\)\n=> 3\n=> 4\n/, out)
381381
end
382382

383383
def test_measure_with_proc_warning
@@ -405,6 +405,32 @@ def test_measure_with_proc_warning
405405
assert_match(/\A=> 3\n=> 3\n/, out)
406406
assert_empty(c.class_variables)
407407
end
408+
409+
def test_legacy_measure_warning
410+
conf = {
411+
MEASURE_PROC: {
412+
FOO: proc {},
413+
BAR: proc {},
414+
}
415+
}
416+
out, err = execute_lines(
417+
"measure :foo\n",
418+
"measure :on, :bar\n",
419+
conf: conf
420+
)
421+
assert_match(/FOO is added/, out)
422+
assert_match(/BAR is added/, out)
423+
assert_match(/`measure :foo` is deprecated. Please use `measure foo`/, err)
424+
assert_match(/`measure :on, :bar` is deprecated. Please use `measure on bar`/, err)
425+
end
426+
427+
def test_unknown_method
428+
out, err = execute_lines("measure foo\n", "measure on bar\n")
429+
assert_empty(err)
430+
assert_match(/`foo` not found/, out)
431+
assert_match(/`bar` not found/, out)
432+
assert_match(/time stackprof/, out)
433+
end
408434
end
409435

410436
class IrbSourceTest < CommandTestCase

0 commit comments

Comments
 (0)