Skip to content

Commit

Permalink
[ruby/irb] Lazily load the multi-irb extension
Browse files Browse the repository at this point in the history
(ruby/irb#472)

* Lazily load the multi-irb extension

We now have plan to implement a command that prints all commands'
information, which will need to load all command files without actually
running them.

But because the `multi-irb` extension patches IRB's top-level methods,
loading it would cause unintentional side-effects.

So this commit moves related requires into command execution to avoid the problem.

* Make extend_irb_context private

Co-authored-by: Takashi Kokubun <takashikkbn@gmail.com>
  • Loading branch information
2 people authored and matzbot committed Dec 7, 2022
1 parent 30c76f4 commit a2d3f56
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions lib/irb/cmd/subirb.rb
Expand Up @@ -10,31 +10,44 @@
#

require_relative "nop"
require_relative "../ext/multi-irb"

module IRB
# :stopdoc:

module ExtendCommand
class IrbCommand < Nop
class MultiIRBCommand < Nop
def initialize(conf)
super
extend_irb_context
end

private

def extend_irb_context
# this extension patches IRB context like IRB.CurrentContext
require_relative "../ext/multi-irb"
end
end

class IrbCommand < MultiIRBCommand
def execute(*obj)
IRB.irb(nil, *obj)
end
end

class Jobs < Nop
class Jobs < MultiIRBCommand
def execute
IRB.JobManager
end
end

class Foreground < Nop
class Foreground < MultiIRBCommand
def execute(key)
IRB.JobManager.switch(key)
end
end

class Kill < Nop
class Kill < MultiIRBCommand
def execute(*keys)
IRB.JobManager.kill(*keys)
end
Expand Down

0 comments on commit a2d3f56

Please sign in to comment.