Skip to content

Commit 1ca7472

Browse files
authored
Fallback to Reline when require 'readline' fails (#1076)
Require readline may fail because it is a bundled gem in 3.5.0.dev.
1 parent 5623f0a commit 1ca7472

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

lib/irb/input-method.rb

+12-7
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,15 @@ def close
175175
class ReadlineInputMethod < StdioInputMethod
176176
class << self
177177
def initialize_readline
178-
require "readline"
179-
rescue LoadError
180-
else
181-
include ::Readline
178+
return if defined?(self::Readline)
179+
180+
begin
181+
require 'readline'
182+
const_set(:Readline, ::Readline)
183+
rescue LoadError
184+
const_set(:Readline, ::Reline)
185+
end
186+
const_set(:HISTORY, self::Readline::HISTORY)
182187
end
183188
end
184189

@@ -216,8 +221,8 @@ def completion_info
216221
def gets
217222
Readline.input = @stdin
218223
Readline.output = @stdout
219-
if l = readline(@prompt, false)
220-
HISTORY.push(l) if !l.empty?
224+
if l = Readline.readline(@prompt, false)
225+
Readline::HISTORY.push(l) if !l.empty?
221226
@line[@line_no += 1] = l + "\n"
222227
else
223228
@eof = true
@@ -239,7 +244,7 @@ def prompting?
239244

240245
# For debug message
241246
def inspect
242-
readline_impl = (defined?(Reline) && Readline == Reline) ? 'Reline' : 'ext/readline'
247+
readline_impl = Readline == ::Reline ? 'Reline' : 'ext/readline'
243248
str = "ReadlineInputMethod with #{readline_impl} #{Readline::VERSION}"
244249
inputrc_path = File.expand_path(ENV['INPUTRC'] || '~/.inputrc')
245250
str += " and #{inputrc_path}" if File.exist?(inputrc_path)

test/irb/test_history.rb

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
# frozen_string_literal: false
22
require 'irb'
3-
require 'readline'
43
require "tempfile"
54

65
require_relative "helper"
76

87
return if RUBY_PLATFORM.match?(/solaris|mswin|mingw/i)
98

109
module TestIRB
10+
begin
11+
require 'readline'
12+
Readline = ::Readline
13+
rescue LoadError
14+
Readline = ::Reline
15+
end
16+
1117
class HistoryTest < TestCase
1218
def setup
1319
@conf_backup = IRB.conf.dup

0 commit comments

Comments
 (0)