[ruby/irb] Group class methods under `class << self`

(https://github.com/ruby/irb/pull/981)

https://github.com/ruby/irb/commit/cdaa356df2
This commit is contained in:
Stan Lo 2024-07-16 16:58:08 +01:00 коммит произвёл git
Родитель 4fe3082b63
Коммит 4a4e1bf357
11 изменённых файлов: 388 добавлений и 373 удалений

Просмотреть файл

@ -880,21 +880,22 @@ module IRB
# An exception raised by IRB.irb_abort
class Abort < Exception;end
class << self
# The current IRB::Context of the session, see IRB.conf
#
# irb
# irb(main):001:0> IRB.CurrentContext.irb_name = "foo"
# foo(main):002:0> IRB.conf[:MAIN_CONTEXT].irb_name #=> "foo"
def IRB.CurrentContext # :nodoc:
IRB.conf[:MAIN_CONTEXT]
def CurrentContext # :nodoc:
conf[:MAIN_CONTEXT]
end
# Initializes IRB and creates a new Irb.irb object at the `TOPLEVEL_BINDING`
def IRB.start(ap_path = nil)
def start(ap_path = nil)
STDOUT.sync = true
$0 = File::basename(ap_path, ".rb") if ap_path
IRB.setup(ap_path)
setup(ap_path)
if @CONF[:SCRIPT]
irb = Irb.new(nil, @CONF[:SCRIPT])
@ -905,16 +906,17 @@ module IRB
end
# Quits irb
def IRB.irb_exit(*) # :nodoc:
def irb_exit(*) # :nodoc:
throw :IRB_EXIT, false
end
# Aborts then interrupts irb.
#
# Will raise an Abort exception, or the given `exception`.
def IRB.irb_abort(irb, exception = Abort) # :nodoc:
def irb_abort(irb, exception = Abort) # :nodoc:
irb.context.thread.raise exception, "abort then interrupt!"
end
end
class Irb
# Note: instance and index assignment expressions could also be written like:

Просмотреть файл

@ -10,9 +10,11 @@ module IRB
module Command
class CommandArgumentError < StandardError; end
def self.extract_ruby_args(*args, **kwargs)
class << self
def extract_ruby_args(*args, **kwargs)
throw :EXTRACT_RUBY_ARGS, [args, kwargs]
end
end
class Base
class << self
@ -31,6 +33,12 @@ module IRB
@help_message
end
def execute(irb_context, arg)
new(irb_context).execute(arg)
rescue CommandArgumentError => e
puts e.message
end
private
def highlight(text)
@ -38,12 +46,6 @@ module IRB
end
end
def self.execute(irb_context, arg)
new(irb_context).execute(arg)
rescue CommandArgumentError => e
puts e.message
end
def initialize(irb_context)
@irb_context = irb_context
end

Просмотреть файл

@ -58,14 +58,16 @@ module IRB
end
class DebugCommand < Debug
def self.category
class << self
def category
"Debugging"
end
def self.description
def description
command_name = self.name.split("::").last.downcase
"Start the debugger of debug.gem and run its `#{command_name}` command."
end
end
end
end
end

Просмотреть файл

@ -259,10 +259,12 @@ module IRB
# Deprecated. Doesn't have any effect.
@EXTEND_COMMANDS = []
class << self
# Drepcated. Use Command.regiser instead.
def self.def_extend_command(cmd_name, cmd_class, _, *aliases)
def def_extend_command(cmd_name, cmd_class, _, *aliases)
Command._register_with_aliases(cmd_name, cmd_class, *aliases)
Command.class_variable_set(:@@command_override_policies, nil)
end
end
end
end

Просмотреть файл

@ -171,12 +171,14 @@ module IRB
end
class ReadlineInputMethod < StdioInputMethod
def self.initialize_readline
class << self
def initialize_readline
require "readline"
rescue LoadError
else
include ::Readline
end
end
include HistorySavingAbility

Просмотреть файл

@ -6,7 +6,6 @@
module IRB # :nodoc:
# Convenience method to create a new Inspector, using the given +inspect+
# proc, and optional +init+ proc and passes them to Inspector.new
#
@ -43,9 +42,10 @@ module IRB # :nodoc:
# +:marshal+:: Using Marshal.dump
INSPECTORS = {}
class << self
# Determines the inspector to use where +inspector+ is one of the keys passed
# during inspector definition.
def self.keys_with_inspector(inspector)
def keys_with_inspector(inspector)
INSPECTORS.select{|k, v| v == inspector}.collect{|k, v| k}
end
@ -55,7 +55,7 @@ module IRB # :nodoc:
# Inspector.def_inspector([key1,..], init_p=nil){|v| v.inspect}
# Inspector.def_inspector(key, inspector)
# Inspector.def_inspector([key1,...], inspector)
def self.def_inspector(key, arg=nil, &block)
def def_inspector(key, arg=nil, &block)
if block_given?
inspector = IRB::Inspector(block, arg)
else
@ -77,6 +77,7 @@ module IRB # :nodoc:
INSPECTORS[key] = inspector
end
end
end
# Creates a new inspector object, using the given +inspect_proc+ when
# output return values in irb.

Просмотреть файл

@ -3,8 +3,9 @@ module IRB
module NestingParser
IGNORE_TOKENS = %i[on_sp on_ignored_nl on_comment on_embdoc_beg on_embdoc on_embdoc_end]
class << self
# Scan each token and call the given block with array of token and other information for parsing
def self.scan_opens(tokens)
def scan_opens(tokens)
opens = []
pending_heredocs = []
first_token_on_line = true
@ -189,7 +190,7 @@ module IRB
opens.map(&:first) + pending_heredocs.reverse
end
def self.open_tokens(tokens)
def open_tokens(tokens)
# scan_opens without block will return a list of open tokens at last token position
scan_opens(tokens)
end
@ -208,7 +209,7 @@ module IRB
# prev_opens: [lbracket, tstring_beg]
# next_tokens: [lbracket, lparen]
# min_depth: 1 (minimum just after tstring_end)
def self.parse_by_line(tokens)
def parse_by_line(tokens)
line_tokens = []
prev_opens = []
min_depth = 0
@ -234,4 +235,5 @@ module IRB
output
end
end
end
end

Просмотреть файл

@ -36,13 +36,30 @@ module IRB
:massign,
]
ERROR_TOKENS = [
:on_parse_error,
:compile_error,
:on_assign_error,
:on_alias_error,
:on_class_name_error,
:on_param_error
]
LTYPE_TOKENS = %i[
on_heredoc_beg on_tstring_beg
on_regexp_beg on_symbeg on_backtick
on_symbols_beg on_qsymbols_beg
on_words_beg on_qwords_beg
]
class TerminateLineInput < StandardError
def initialize
super("Terminate Line Input")
end
end
def self.compile_with_errors_suppressed(code, line_no: 1)
class << self
def compile_with_errors_suppressed(code, line_no: 1)
begin
result = yield code, line_no
rescue ArgumentError
@ -59,23 +76,14 @@ module IRB
result
end
ERROR_TOKENS = [
:on_parse_error,
:compile_error,
:on_assign_error,
:on_alias_error,
:on_class_name_error,
:on_param_error
]
def self.generate_local_variables_assign_code(local_variables)
def generate_local_variables_assign_code(local_variables)
"#{local_variables.join('=')}=nil;" unless local_variables.empty?
end
# Some part of the code is not included in Ripper's token.
# Example: DATA part, token after heredoc_beg when heredoc has unclosed embexpr.
# With interpolated tokens, tokens.map(&:tok).join will be equal to code.
def self.interpolate_ripper_ignored_tokens(code, tokens)
def interpolate_ripper_ignored_tokens(code, tokens)
line_positions = [0]
code.lines.each do |line|
line_positions << line_positions.last + line.bytesize
@ -104,7 +112,7 @@ module IRB
interpolated
end
def self.ripper_lex_without_warning(code, local_variables: [])
def ripper_lex_without_warning(code, local_variables: [])
verbose, $VERBOSE = $VERBOSE, nil
lvars_code = generate_local_variables_assign_code(local_variables)
original_code = code
@ -133,6 +141,7 @@ module IRB
ensure
$VERBOSE = verbose
end
end
def check_code_state(code, local_variables:)
tokens = self.class.ripper_lex_without_warning(code, local_variables: local_variables)
@ -391,13 +400,6 @@ module IRB
end
end
LTYPE_TOKENS = %i[
on_heredoc_beg on_tstring_beg
on_regexp_beg on_symbeg on_backtick
on_symbols_beg on_qsymbols_beg
on_words_beg on_qwords_beg
]
def ltype_from_open_tokens(opens)
start_token = opens.reverse_each.find do |tok|
LTYPE_TOKENS.include?(tok.event)

Просмотреть файл

@ -176,13 +176,15 @@ EOF
end
module HelpersContainer
def self.install_helper_methods
class << self
def install_helper_methods
HelperMethod.helper_methods.each do |name, helper_method_class|
define_method name do |*args, **opts, &block|
helper_method_class.instance.execute(*args, **opts, &block)
end unless method_defined?(name)
end
end
end
install_helper_methods
end

Просмотреть файл

@ -28,7 +28,6 @@ module TestIRB
restore_encodings
end
def test_eval_input
verbose, $VERBOSE = $VERBOSE, nil
input = TestInputMethod.new([

Просмотреть файл

@ -80,7 +80,6 @@ module TestIRB
assert_equal(nil, workspace.code_around_binding)
end
def test_toplevel_binding_local_variables
bug17623 = '[ruby-core:102468]'
bundle_exec = ENV.key?('BUNDLE_GEMFILE') ? ['-rbundler/setup'] : []