Skip to content

Commit

Permalink
Group class methods under class << self (#981)
Browse files Browse the repository at this point in the history
  • Loading branch information
st0012 authored Jul 16, 2024
1 parent c1be413 commit cdaa356
Show file tree
Hide file tree
Showing 12 changed files with 386 additions and 364 deletions.
7 changes: 7 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,10 @@ Layout/SpaceBeforeComma:

Layout/SpaceAfterComma:
Enabled: true

Style/ClassMethodsDefinitions:
Enabled: true
EnforcedStyle: self_class

Layout/EmptyLines:
Enabled: true
58 changes: 30 additions & 28 deletions lib/irb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -880,40 +880,42 @@ module IRB
# An exception raised by IRB.irb_abort
class Abort < Exception;end

# 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]
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 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)
STDOUT.sync = true
$0 = File::basename(ap_path, ".rb") if ap_path
# Initializes IRB and creates a new Irb.irb object at the `TOPLEVEL_BINDING`
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])
else
irb = Irb.new
if @CONF[:SCRIPT]
irb = Irb.new(nil, @CONF[:SCRIPT])
else
irb = Irb.new
end
irb.run(@CONF)
end
irb.run(@CONF)
end

# Quits irb
def IRB.irb_exit(*) # :nodoc:
throw :IRB_EXIT, false
end
# Quits irb
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:
irb.context.thread.raise exception, "abort then interrupt!"
# Aborts then interrupts irb.
#
# Will raise an Abort exception, or the given `exception`.
def irb_abort(irb, exception = Abort) # :nodoc:
irb.context.thread.raise exception, "abort then interrupt!"
end
end

class Irb
Expand Down
18 changes: 10 additions & 8 deletions lib/irb/command/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ module IRB
module Command
class CommandArgumentError < StandardError; end

def self.extract_ruby_args(*args, **kwargs)
throw :EXTRACT_RUBY_ARGS, [args, kwargs]
class << self
def extract_ruby_args(*args, **kwargs)
throw :EXTRACT_RUBY_ARGS, [args, kwargs]
end
end

class Base
Expand All @@ -31,19 +33,19 @@ def help_message(help_message = nil)
@help_message
end

def execute(irb_context, arg)
new(irb_context).execute(arg)
rescue CommandArgumentError => e
puts e.message
end

private

def highlight(text)
Color.colorize(text, [:BOLD, :BLUE])
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
Expand Down
14 changes: 8 additions & 6 deletions lib/irb/command/debug.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,15 @@ def execute_debug_command(pre_cmds: nil, do_cmds: nil)
end

class DebugCommand < Debug
def self.category
"Debugging"
end
class << self
def category
"Debugging"
end

def self.description
command_name = self.name.split("::").last.downcase
"Start the debugger of debug.gem and run its `#{command_name}` command."
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
Expand Down
10 changes: 6 additions & 4 deletions lib/irb/default_commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -259,10 +259,12 @@ module ExtendCommandBundle
# Deprecated. Doesn't have any effect.
@EXTEND_COMMANDS = []

# Drepcated. Use Command.regiser instead.
def self.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)
class << self
# Drepcated. Use Command.regiser instead.
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
12 changes: 7 additions & 5 deletions lib/irb/input-method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,13 @@ def close
end

class ReadlineInputMethod < StdioInputMethod
def self.initialize_readline
require "readline"
rescue LoadError
else
include ::Readline
class << self
def initialize_readline
require "readline"
rescue LoadError
else
include ::Readline
end
end

include HistorySavingAbility
Expand Down
61 changes: 31 additions & 30 deletions lib/irb/inspector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
#
Expand Down Expand Up @@ -43,38 +42,40 @@ class Inspector
# +:marshal+:: Using Marshal.dump
INSPECTORS = {}

# Determines the inspector to use where +inspector+ is one of the keys passed
# during inspector definition.
def self.keys_with_inspector(inspector)
INSPECTORS.select{|k, v| v == inspector}.collect{|k, v| k}
end

# Example
#
# Inspector.def_inspector(key, init_p=nil){|v| v.inspect}
# 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)
if block_given?
inspector = IRB::Inspector(block, arg)
else
inspector = arg
class << self
# Determines the inspector to use where +inspector+ is one of the keys passed
# during inspector definition.
def keys_with_inspector(inspector)
INSPECTORS.select{|k, v| v == inspector}.collect{|k, v| k}
end

case key
when Array
for k in key
def_inspector(k, inspector)
# Example
#
# Inspector.def_inspector(key, init_p=nil){|v| v.inspect}
# Inspector.def_inspector([key1,..], init_p=nil){|v| v.inspect}
# Inspector.def_inspector(key, inspector)
# Inspector.def_inspector([key1,...], inspector)
def def_inspector(key, arg=nil, &block)
if block_given?
inspector = IRB::Inspector(block, arg)
else
inspector = arg
end

case key
when Array
for k in key
def_inspector(k, inspector)
end
when Symbol
INSPECTORS[key] = inspector
INSPECTORS[key.to_s] = inspector
when String
INSPECTORS[key] = inspector
INSPECTORS[key.intern] = inspector
else
INSPECTORS[key] = inspector
end
when Symbol
INSPECTORS[key] = inspector
INSPECTORS[key.to_s] = inspector
when String
INSPECTORS[key] = inspector
INSPECTORS[key.intern] = inspector
else
INSPECTORS[key] = inspector
end
end

Expand Down
Loading

0 comments on commit cdaa356

Please sign in to comment.