From 1ca7472f76cb05a67c86c21fdf90ad3712c9c043 Mon Sep 17 00:00:00 2001
From: tomoya ishida <tomoyapenguin@gmail.com>
Date: Mon, 27 Jan 2025 23:01:02 +0900
Subject: [PATCH] Fallback to Reline when `require 'readline'` fails (#1076)

Require readline may fail because it is a bundled gem in 3.5.0.dev.
---
 lib/irb/input-method.rb  | 19 ++++++++++++-------
 test/irb/test_history.rb |  8 +++++++-
 2 files changed, 19 insertions(+), 8 deletions(-)

diff --git a/lib/irb/input-method.rb b/lib/irb/input-method.rb
index 260d9a1cb..f7f0c80ae 100644
--- a/lib/irb/input-method.rb
+++ b/lib/irb/input-method.rb
@@ -175,10 +175,15 @@ def close
   class ReadlineInputMethod < StdioInputMethod
     class << self
       def initialize_readline
-        require "readline"
-      rescue LoadError
-      else
-        include ::Readline
+        return if defined?(self::Readline)
+
+        begin
+          require 'readline'
+          const_set(:Readline, ::Readline)
+        rescue LoadError
+          const_set(:Readline, ::Reline)
+        end
+        const_set(:HISTORY, self::Readline::HISTORY)
       end
     end
 
@@ -216,8 +221,8 @@ def completion_info
     def gets
       Readline.input = @stdin
       Readline.output = @stdout
-      if l = readline(@prompt, false)
-        HISTORY.push(l) if !l.empty?
+      if l = Readline.readline(@prompt, false)
+        Readline::HISTORY.push(l) if !l.empty?
         @line[@line_no += 1] = l + "\n"
       else
         @eof = true
@@ -239,7 +244,7 @@ def prompting?
 
     # For debug message
     def inspect
-      readline_impl = (defined?(Reline) && Readline == Reline) ? 'Reline' : 'ext/readline'
+      readline_impl = Readline == ::Reline ? 'Reline' : 'ext/readline'
       str = "ReadlineInputMethod with #{readline_impl} #{Readline::VERSION}"
       inputrc_path = File.expand_path(ENV['INPUTRC'] || '~/.inputrc')
       str += " and #{inputrc_path}" if File.exist?(inputrc_path)
diff --git a/test/irb/test_history.rb b/test/irb/test_history.rb
index 0171bb0ec..77b680d91 100644
--- a/test/irb/test_history.rb
+++ b/test/irb/test_history.rb
@@ -1,6 +1,5 @@
 # frozen_string_literal: false
 require 'irb'
-require 'readline'
 require "tempfile"
 
 require_relative "helper"
@@ -8,6 +7,13 @@
 return if RUBY_PLATFORM.match?(/solaris|mswin|mingw/i)
 
 module TestIRB
+  begin
+    require 'readline'
+    Readline = ::Readline
+  rescue LoadError
+    Readline = ::Reline
+  end
+
   class HistoryTest < TestCase
     def setup
       @conf_backup = IRB.conf.dup