Skip to content

Commit fc6cd4d

Browse files
committed
WIP
1 parent 629e65d commit fc6cd4d

File tree

6 files changed

+63
-25
lines changed

6 files changed

+63
-25
lines changed

lib/irb/cmd/help.rb

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,6 @@ module IRB
1010

1111
module ExtendCommand
1212
class Help < Nop
13-
class << self
14-
def transform_args(args)
15-
# Return a string literal as is for backward compatibility
16-
if args.empty? || string_literal?(args)
17-
args
18-
else # Otherwise, consider the input as a String for convenience
19-
args.strip.dump
20-
end
21-
end
22-
end
23-
2413
category "Context"
2514
description "Enter the mode to look up RI documents."
2615

lib/irb/cmd/irb_info.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class IrbInfo < Nop
1111
description "Show information about IRB."
1212

1313
def execute
14-
Class.new {
14+
c = Class.new {
1515
def inspect
1616
str = "Ruby version: #{RUBY_VERSION}\n"
1717
str += "IRB version: #{IRB.version}\n"
@@ -29,6 +29,8 @@ def inspect
2929
end
3030
alias_method :to_s, :inspect
3131
}.new
32+
puts c
33+
c
3234
end
3335
end
3436
end

lib/irb/cmd/ls.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,26 @@ def self.transform_args(args)
2121
end
2222
end
2323

24+
def execute_with_raw_args(raw_args)
25+
raw_args = raw_args.strip
26+
27+
if raw_args.empty?
28+
execute
29+
else
30+
if match = raw_args.match(/\A(?<args>.+\s|)(-g|-G)\s+(?<grep>[^\s]+)\s*\z/)
31+
args = match[:args]
32+
33+
if !args.empty?
34+
execute(evaluate(args), grep: /#{match[:grep]}/)
35+
else
36+
execute(grep: /#{match[:grep]}/)
37+
end
38+
else
39+
execute(evaluate(raw_args))
40+
end
41+
end
42+
end
43+
2444
def execute(*arg, grep: nil)
2545
o = Output.new(grep: grep)
2646

lib/irb/cmd/nop.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,41 @@ def irb
5050
def execute(*opts)
5151
#nop
5252
end
53+
54+
def transform_args(raw_args)
55+
if string_literal?(raw_args)
56+
evaluate(raw_args)
57+
else
58+
raw_args
59+
end
60+
end
61+
62+
def execute_with_raw_args(raw_args)
63+
if raw_args.nil? || raw_args.empty?
64+
execute
65+
else
66+
raw_args = raw_args.strip
67+
68+
args =
69+
if respond_to?(:transform_args)
70+
transform_args(raw_args)
71+
else
72+
evaluate(raw_args)
73+
end
74+
execute(args)
75+
end
76+
end
77+
78+
private
79+
80+
def string_literal?(args)
81+
sexp = Ripper.sexp(args)
82+
sexp && sexp.size == 2 && sexp.last&.first&.first == :string_literal
83+
end
84+
85+
def evaluate(str)
86+
eval(str, @irb_context.workspace.binding)
87+
end
5388
end
5489
end
5590

lib/irb/cmd/show_source.rb

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,6 @@ class ShowSource < Nop
1313
description "Show the source code of a given method or constant."
1414

1515
class << self
16-
def transform_args(args)
17-
# Return a string literal as is for backward compatibility
18-
if args.empty? || string_literal?(args)
19-
args
20-
else # Otherwise, consider the input as a String for convenience
21-
args.strip.dump
22-
end
23-
end
24-
2516
def find_source(str, irb_context)
2617
case str
2718
when /\A[A-Z]\w*(::[A-Z]\w*)*\z/ # Const::Name

lib/irb/context.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -490,11 +490,12 @@ def evaluate(line, line_no, exception: nil) # :nodoc:
490490

491491
# Hook command-specific transformation
492492
command_class = ExtendCommandBundle.load_command(command)
493-
if command_class&.respond_to?(:transform_args)
494-
line = "#{command} #{command_class.transform_args(args)}"
495-
end
496493

497-
set_last_value(@workspace.evaluate(line, irb_path, line_no))
494+
if command_class
495+
command_class.new(self).execute_with_raw_args(args)
496+
else
497+
set_last_value(@workspace.evaluate(line, irb_path, line_no))
498+
end
498499
end
499500

500501
def inspect_last_value # :nodoc:

0 commit comments

Comments
 (0)