Skip to content

Commit caddd02

Browse files
st0012matzbot
authored andcommitted
[ruby/irb] Move input line mutation out of Context#evaluate
(ruby/irb#615) This makes sure `Context#evaluate` really just evaluates the input. It will also make #575's implementation cleaner.
1 parent 913e01e commit caddd02

File tree

2 files changed

+21
-16
lines changed

2 files changed

+21
-16
lines changed

lib/irb.rb

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -563,11 +563,11 @@ def eval_input
563563
if IRB.conf[:MEASURE] && IRB.conf[:MEASURE_CALLBACKS].empty?
564564
IRB.set_measure_callback
565565
end
566-
# Assignment expression check should be done before @context.evaluate to handle code like `a /2#/ if false; a = 1`
566+
# Assignment expression check should be done before evaluate_line to handle code like `a /2#/ if false; a = 1`
567567
is_assignment = assignment_expression?(line)
568568
if IRB.conf[:MEASURE] && !IRB.conf[:MEASURE_CALLBACKS].empty?
569569
result = nil
570-
last_proc = proc{ result = @context.evaluate(line, line_no, exception: exc) }
570+
last_proc = proc{ result = evaluate_line(line, line_no, exception: exc) }
571571
IRB.conf[:MEASURE_CALLBACKS].inject(last_proc) { |chain, item|
572572
_name, callback, arg = item
573573
proc {
@@ -578,7 +578,7 @@ def eval_input
578578
}.call
579579
@context.set_last_value(result)
580580
else
581-
@context.evaluate(line, line_no, exception: exc)
581+
evaluate_line(line, line_no, exception: exc)
582582
end
583583
if @context.echo?
584584
if is_assignment
@@ -604,6 +604,23 @@ def eval_input
604604
end
605605
end
606606

607+
def evaluate_line(line, line_no, exception: nil)
608+
# Transform a non-identifier alias (@, $) or keywords (next, break)
609+
command, args = line.split(/\s/, 2)
610+
if original = @context.command_aliases[command.to_sym]
611+
line = line.gsub(/\A#{Regexp.escape(command)}/, original.to_s)
612+
command = original
613+
end
614+
615+
# Hook command-specific transformation
616+
command_class = ExtendCommandBundle.load_command(command)
617+
if command_class&.respond_to?(:transform_args)
618+
line = "#{command} #{command_class.transform_args(args)}"
619+
end
620+
621+
@context.evaluate(line, line_no, exception: exception)
622+
end
623+
607624
def convert_invalid_byte_sequence(str, enc)
608625
str.force_encoding(enc)
609626
str.scrub { |c|

lib/irb/context.rb

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -475,25 +475,13 @@ def inspect_mode=(opt)
475475

476476
def evaluate(line, line_no, exception: nil) # :nodoc:
477477
@line_no = line_no
478+
478479
if exception
479480
line_no -= 1
480481
line = "begin ::Kernel.raise _; rescue _.class\n#{line}\n""end"
481482
@workspace.local_variable_set(:_, exception)
482483
end
483484

484-
# Transform a non-identifier alias (@, $) or keywords (next, break)
485-
command, args = line.split(/\s/, 2)
486-
if original = command_aliases[command.to_sym]
487-
line = line.gsub(/\A#{Regexp.escape(command)}/, original.to_s)
488-
command = original
489-
end
490-
491-
# Hook command-specific transformation
492-
command_class = ExtendCommandBundle.load_command(command)
493-
if command_class&.respond_to?(:transform_args)
494-
line = "#{command} #{command_class.transform_args(args)}"
495-
end
496-
497485
set_last_value(@workspace.evaluate(line, irb_path, line_no))
498486
end
499487

0 commit comments

Comments
 (0)