Skip to content

Commit 7405a84

Browse files
tompngst0012
andauthored
Remove internal-only methods from Command::Base (#922)
* Remove internal-only methods from Command::Base Command#ruby_args and Command#unwrap_string_literal are used for default command's argument backward compatibility. Moved these methods to another module to avoid being used from custom commands. * Update lib/irb/command/edit.rb --------- Co-authored-by: Stan Lo <stan001212@gmail.com>
1 parent 8886434 commit 7405a84

10 files changed

Lines changed: 41 additions & 19 deletions

File tree

‎lib/irb/command/base.rb‎

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -50,25 +50,6 @@ def initialize(irb_context)
5050

5151
attr_reader :irb_context
5252

53-
def unwrap_string_literal(str)
54-
return if str.empty?
55-
56-
sexp = Ripper.sexp(str)
57-
if sexp && sexp.size == 2 && sexp.last&.first&.first == :string_literal
58-
@irb_context.workspace.binding.eval(str).to_s
59-
else
60-
str
61-
end
62-
end
63-
64-
def ruby_args(arg)
65-
# Use throw and catch to handle arg that includes `;`
66-
# For example: "1, kw: (2; 3); 4" will be parsed to [[1], { kw: 3 }]
67-
catch(:EXTRACT_RUBY_ARGS) do
68-
@irb_context.workspace.binding.eval "IRB::Command.extract_ruby_args #{arg}"
69-
end || [[], {}]
70-
end
71-
7253
def execute(arg)
7354
#nop
7455
end

‎lib/irb/command/edit.rb‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ module IRB
88

99
module Command
1010
class Edit < Base
11+
include RubyArgsExtractor
12+
1113
category "Misc"
1214
description 'Open a file or source location.'
1315
help_message <<~HELP_MESSAGE
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# frozen_string_literal: true
2+
3+
module IRB
4+
module Command
5+
# Internal use only, for default command's backward compatibility.
6+
module RubyArgsExtractor # :nodoc:
7+
def unwrap_string_literal(str)
8+
return if str.empty?
9+
10+
sexp = Ripper.sexp(str)
11+
if sexp && sexp.size == 2 && sexp.last&.first&.first == :string_literal
12+
@irb_context.workspace.binding.eval(str).to_s
13+
else
14+
str
15+
end
16+
end
17+
18+
def ruby_args(arg)
19+
# Use throw and catch to handle arg that includes `;`
20+
# For example: "1, kw: (2; 3); 4" will be parsed to [[1], { kw: 3 }]
21+
catch(:EXTRACT_RUBY_ARGS) do
22+
@irb_context.workspace.binding.eval "IRB::Command.extract_ruby_args #{arg}"
23+
end || [[], {}]
24+
end
25+
end
26+
end
27+
end

‎lib/irb/command/load.rb‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ module IRB
1010

1111
module Command
1212
class LoaderCommand < Base
13+
include RubyArgsExtractor
1314
include IrbLoader
1415

1516
def raise_cmd_argument_error

‎lib/irb/command/ls.rb‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ module IRB
1111

1212
module Command
1313
class Ls < Base
14+
include RubyArgsExtractor
15+
1416
category "Context"
1517
description "Show methods, constants, and variables."
1618

‎lib/irb/command/measure.rb‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ module IRB
33

44
module Command
55
class Measure < Base
6+
include RubyArgsExtractor
7+
68
category "Misc"
79
description "`measure` enables the mode to measure processing time. `measure :off` disables it."
810

‎lib/irb/command/show_doc.rb‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
module IRB
44
module Command
55
class ShowDoc < Base
6+
include RubyArgsExtractor
7+
68
category "Context"
79
description "Look up documentation with RI."
810

‎lib/irb/command/show_source.rb‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
module IRB
88
module Command
99
class ShowSource < Base
10+
include RubyArgsExtractor
11+
1012
category "Context"
1113
description "Show the source code of a given method, class/module, or constant."
1214

‎lib/irb/command/subirb.rb‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ module IRB
99

1010
module Command
1111
class MultiIRBCommand < Base
12+
include RubyArgsExtractor
13+
1214
private
1315

1416
def print_deprecated_warning

‎lib/irb/default_commands.rb‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# frozen_string_literal: true
22

33
require_relative "command"
4+
require_relative "command/internal_helpers"
45
require_relative "command/context"
56
require_relative "command/exit"
67
require_relative "command/force_exit"

0 commit comments

Comments
 (0)