From a2d3f5606a241999feda113f7331cf1a637bcaf0 Mon Sep 17 00:00:00 2001 From: Stan Lo Date: Wed, 7 Dec 2022 23:29:55 +0000 Subject: [PATCH] [ruby/irb] Lazily load the multi-irb extension (https://github.com/ruby/irb/pull/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 --- lib/irb/cmd/subirb.rb | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/lib/irb/cmd/subirb.rb b/lib/irb/cmd/subirb.rb index b322aadc53..4d113c5bd7 100644 --- a/lib/irb/cmd/subirb.rb +++ b/lib/irb/cmd/subirb.rb @@ -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