Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 4 additions & 10 deletions lib/irb/cmd/measure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def initialize(*args)
super(*args)
end

def execute(type = nil, arg = nil, &block)
def execute(type = nil, arg = nil)
# Please check IRB.init_config in lib/irb/init.rb that sets
# IRB.conf[:MEASURE_PROC] to register default "measure" methods,
# "measure :time" (abbreviated as "measure") and "measure :stackprof".
Expand All @@ -29,15 +29,9 @@ def execute(type = nil, arg = nil, &block)
added = IRB.set_measure_callback(type, arg)
puts "#{added[0]} is added." if added
else
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
IRB.conf[:MEASURE] = true
added = IRB.set_measure_callback(type, arg)
puts "#{added[0]} is added." if added
end
nil
end
Expand Down
4 changes: 2 additions & 2 deletions lib/irb/cmd/nop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ def string_literal?(args)
end
end

def self.execute(irb_context, *opts, **kwargs, &block)
def self.execute(irb_context, *opts, **kwargs)
command = new(irb_context)
command.execute(*opts, **kwargs, &block)
command.execute(*opts, **kwargs)
rescue CommandArgumentError => e
puts e.message
end
Expand Down
34 changes: 28 additions & 6 deletions lib/irb/extend-command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -259,12 +259,34 @@ def self.def_extend_command(cmd_name, cmd_class, load_file, *aliases)
cmd_class = cmd_class.name
end

line = __LINE__; eval %[
def #{cmd_name}(*opts, **kwargs, &b)
Kernel.require_relative "#{load_file}"
::IRB::ExtendCommand::#{cmd_class}.execute(irb_context, *opts, **kwargs, &b)
end
], nil, __FILE__, line
# TODO: Remove this condition after 2.0
if cmd_name == :irb_measure
line = __LINE__; eval %[
def irb_measure(type = nil, arg = nil, &block)
if block
warn <<~MSG
Passing a block to measure is deprecated and will be removed in the next IRB major release.
Please activate measuring with `IRB.conf[:MEASURE] = true`, and set the callback with `IRB.set_measure_callback(&block)` instead.
MSG

IRB.conf[:MEASURE] = true
added = IRB.set_measure_callback(&block)
puts "\#{added[0]} is added." if added
else
Kernel.require_relative "cmd/measure"
::IRB::ExtendCommand::Measure.execute(irb_context, type, arg)
end
end
], nil, __FILE__, line
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this is not pretty, but this is the only way I can think of to make command refactoring easy while not introducing a n immediate breaking change.

else
line = __LINE__; eval %[
def #{cmd_name}(*opts, **kwargs, &b)
Kernel.require_relative "#{load_file}"
::IRB::ExtendCommand::#{cmd_class}.execute(irb_context, *opts, **kwargs, &b)
end
], nil, __FILE__, line
end


for ali, flag in aliases
@ALIASES.push [ali, cmd_name, flag]
Expand Down
32 changes: 31 additions & 1 deletion test/irb/test_cmd.rb
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,36 @@ def test_measure
assert_match(/\A=> 3\nTIME is added\.\n=> nil\nprocessing time: .+\n=> 3\n=> nil\n=> 3\n/, out)
assert_empty(c.class_variables)
end
def test_measure_doesnt_override
conf = {
PROMPT: {
DEFAULT: {
PROMPT_I: '> ',
PROMPT_S: '> ',
PROMPT_C: '> ',
PROMPT_N: '> '
}
},
PROMPT_MODE: :DEFAULT,
MEASURE: false
}

c = Class.new(Object) do
def measure(*)
puts "measure defined by user"
end
end

out, err = execute_lines(
"measure :on\n",
conf: conf,
main: c.new
)

assert_empty err
assert_match(/measure defined by user/, out)
assert_empty(c.class_variables)
end

def test_measure_enabled_by_rc
conf = {
Expand Down Expand Up @@ -375,7 +405,7 @@ def test_measure_with_proc
main: c
)

assert_empty err
assert_match(/Passing a block to measure is deprecated/, err)
assert_match(/\A=> 3\nBLOCK is added\.\n=> nil\naaa\n=> 3\nBLOCK is added.\naaa\n=> nil\nbbb\n=> 3\n=> nil\n=> 3\n/, out)
assert_empty(c.class_variables)
end
Expand Down