Skip to content

Commit cdaa356

Browse files
authored
Group class methods under class << self (#981)
1 parent c1be413 commit cdaa356

File tree

12 files changed

+386
-364
lines changed

12 files changed

+386
-364
lines changed

.rubocop.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,10 @@ Layout/SpaceBeforeComma:
3030

3131
Layout/SpaceAfterComma:
3232
Enabled: true
33+
34+
Style/ClassMethodsDefinitions:
35+
Enabled: true
36+
EnforcedStyle: self_class
37+
38+
Layout/EmptyLines:
39+
Enabled: true

lib/irb.rb

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -880,40 +880,42 @@ module IRB
880880
# An exception raised by IRB.irb_abort
881881
class Abort < Exception;end
882882

883-
# The current IRB::Context of the session, see IRB.conf
884-
#
885-
# irb
886-
# irb(main):001:0> IRB.CurrentContext.irb_name = "foo"
887-
# foo(main):002:0> IRB.conf[:MAIN_CONTEXT].irb_name #=> "foo"
888-
def IRB.CurrentContext # :nodoc:
889-
IRB.conf[:MAIN_CONTEXT]
890-
end
883+
class << self
884+
# The current IRB::Context of the session, see IRB.conf
885+
#
886+
# irb
887+
# irb(main):001:0> IRB.CurrentContext.irb_name = "foo"
888+
# foo(main):002:0> IRB.conf[:MAIN_CONTEXT].irb_name #=> "foo"
889+
def CurrentContext # :nodoc:
890+
conf[:MAIN_CONTEXT]
891+
end
891892

892-
# Initializes IRB and creates a new Irb.irb object at the `TOPLEVEL_BINDING`
893-
def IRB.start(ap_path = nil)
894-
STDOUT.sync = true
895-
$0 = File::basename(ap_path, ".rb") if ap_path
893+
# Initializes IRB and creates a new Irb.irb object at the `TOPLEVEL_BINDING`
894+
def start(ap_path = nil)
895+
STDOUT.sync = true
896+
$0 = File::basename(ap_path, ".rb") if ap_path
896897

897-
IRB.setup(ap_path)
898+
setup(ap_path)
898899

899-
if @CONF[:SCRIPT]
900-
irb = Irb.new(nil, @CONF[:SCRIPT])
901-
else
902-
irb = Irb.new
900+
if @CONF[:SCRIPT]
901+
irb = Irb.new(nil, @CONF[:SCRIPT])
902+
else
903+
irb = Irb.new
904+
end
905+
irb.run(@CONF)
903906
end
904-
irb.run(@CONF)
905-
end
906907

907-
# Quits irb
908-
def IRB.irb_exit(*) # :nodoc:
909-
throw :IRB_EXIT, false
910-
end
908+
# Quits irb
909+
def irb_exit(*) # :nodoc:
910+
throw :IRB_EXIT, false
911+
end
911912

912-
# Aborts then interrupts irb.
913-
#
914-
# Will raise an Abort exception, or the given `exception`.
915-
def IRB.irb_abort(irb, exception = Abort) # :nodoc:
916-
irb.context.thread.raise exception, "abort then interrupt!"
913+
# Aborts then interrupts irb.
914+
#
915+
# Will raise an Abort exception, or the given `exception`.
916+
def irb_abort(irb, exception = Abort) # :nodoc:
917+
irb.context.thread.raise exception, "abort then interrupt!"
918+
end
917919
end
918920

919921
class Irb

lib/irb/command/base.rb

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ module IRB
1010
module Command
1111
class CommandArgumentError < StandardError; end
1212

13-
def self.extract_ruby_args(*args, **kwargs)
14-
throw :EXTRACT_RUBY_ARGS, [args, kwargs]
13+
class << self
14+
def extract_ruby_args(*args, **kwargs)
15+
throw :EXTRACT_RUBY_ARGS, [args, kwargs]
16+
end
1517
end
1618

1719
class Base
@@ -31,19 +33,19 @@ def help_message(help_message = nil)
3133
@help_message
3234
end
3335

36+
def execute(irb_context, arg)
37+
new(irb_context).execute(arg)
38+
rescue CommandArgumentError => e
39+
puts e.message
40+
end
41+
3442
private
3543

3644
def highlight(text)
3745
Color.colorize(text, [:BOLD, :BLUE])
3846
end
3947
end
4048

41-
def self.execute(irb_context, arg)
42-
new(irb_context).execute(arg)
43-
rescue CommandArgumentError => e
44-
puts e.message
45-
end
46-
4749
def initialize(irb_context)
4850
@irb_context = irb_context
4951
end

lib/irb/command/debug.rb

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,15 @@ def execute_debug_command(pre_cmds: nil, do_cmds: nil)
5858
end
5959

6060
class DebugCommand < Debug
61-
def self.category
62-
"Debugging"
63-
end
61+
class << self
62+
def category
63+
"Debugging"
64+
end
6465

65-
def self.description
66-
command_name = self.name.split("::").last.downcase
67-
"Start the debugger of debug.gem and run its `#{command_name}` command."
66+
def description
67+
command_name = self.name.split("::").last.downcase
68+
"Start the debugger of debug.gem and run its `#{command_name}` command."
69+
end
6870
end
6971
end
7072
end

lib/irb/default_commands.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -259,10 +259,12 @@ module ExtendCommandBundle
259259
# Deprecated. Doesn't have any effect.
260260
@EXTEND_COMMANDS = []
261261

262-
# Drepcated. Use Command.regiser instead.
263-
def self.def_extend_command(cmd_name, cmd_class, _, *aliases)
264-
Command._register_with_aliases(cmd_name, cmd_class, *aliases)
265-
Command.class_variable_set(:@@command_override_policies, nil)
262+
class << self
263+
# Drepcated. Use Command.regiser instead.
264+
def def_extend_command(cmd_name, cmd_class, _, *aliases)
265+
Command._register_with_aliases(cmd_name, cmd_class, *aliases)
266+
Command.class_variable_set(:@@command_override_policies, nil)
267+
end
266268
end
267269
end
268270
end

lib/irb/input-method.rb

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -171,11 +171,13 @@ def close
171171
end
172172

173173
class ReadlineInputMethod < StdioInputMethod
174-
def self.initialize_readline
175-
require "readline"
176-
rescue LoadError
177-
else
178-
include ::Readline
174+
class << self
175+
def initialize_readline
176+
require "readline"
177+
rescue LoadError
178+
else
179+
include ::Readline
180+
end
179181
end
180182

181183
include HistorySavingAbility

lib/irb/inspector.rb

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
module IRB # :nodoc:
88

9-
109
# Convenience method to create a new Inspector, using the given +inspect+
1110
# proc, and optional +init+ proc and passes them to Inspector.new
1211
#
@@ -43,38 +42,40 @@ class Inspector
4342
# +:marshal+:: Using Marshal.dump
4443
INSPECTORS = {}
4544

46-
# Determines the inspector to use where +inspector+ is one of the keys passed
47-
# during inspector definition.
48-
def self.keys_with_inspector(inspector)
49-
INSPECTORS.select{|k, v| v == inspector}.collect{|k, v| k}
50-
end
51-
52-
# Example
53-
#
54-
# Inspector.def_inspector(key, init_p=nil){|v| v.inspect}
55-
# Inspector.def_inspector([key1,..], init_p=nil){|v| v.inspect}
56-
# Inspector.def_inspector(key, inspector)
57-
# Inspector.def_inspector([key1,...], inspector)
58-
def self.def_inspector(key, arg=nil, &block)
59-
if block_given?
60-
inspector = IRB::Inspector(block, arg)
61-
else
62-
inspector = arg
45+
class << self
46+
# Determines the inspector to use where +inspector+ is one of the keys passed
47+
# during inspector definition.
48+
def keys_with_inspector(inspector)
49+
INSPECTORS.select{|k, v| v == inspector}.collect{|k, v| k}
6350
end
6451

65-
case key
66-
when Array
67-
for k in key
68-
def_inspector(k, inspector)
52+
# Example
53+
#
54+
# Inspector.def_inspector(key, init_p=nil){|v| v.inspect}
55+
# Inspector.def_inspector([key1,..], init_p=nil){|v| v.inspect}
56+
# Inspector.def_inspector(key, inspector)
57+
# Inspector.def_inspector([key1,...], inspector)
58+
def def_inspector(key, arg=nil, &block)
59+
if block_given?
60+
inspector = IRB::Inspector(block, arg)
61+
else
62+
inspector = arg
63+
end
64+
65+
case key
66+
when Array
67+
for k in key
68+
def_inspector(k, inspector)
69+
end
70+
when Symbol
71+
INSPECTORS[key] = inspector
72+
INSPECTORS[key.to_s] = inspector
73+
when String
74+
INSPECTORS[key] = inspector
75+
INSPECTORS[key.intern] = inspector
76+
else
77+
INSPECTORS[key] = inspector
6978
end
70-
when Symbol
71-
INSPECTORS[key] = inspector
72-
INSPECTORS[key.to_s] = inspector
73-
when String
74-
INSPECTORS[key] = inspector
75-
INSPECTORS[key.intern] = inspector
76-
else
77-
INSPECTORS[key] = inspector
7879
end
7980
end
8081

0 commit comments

Comments
 (0)