Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Fallback to Reline when require 'readline' fails
Require readline may fail because it is a bundled gem in 3.5.0.dev.
  • Loading branch information
tompng committed Jan 26, 2025
commit 77d6069d663f0f86298c6c49ab99c714191686eb
19 changes: 12 additions & 7 deletions lib/irb/input-method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adds ReadlineInputMethod::Readline = ::Readline or ::Reline
because we don't want to pollute the top level by adding constant ::Readline when require 'readline' fails.

const_set(:HISTORY, self::Readline::HISTORY)
end
end

Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
8 changes: 7 additions & 1 deletion test/irb/test_history.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
# frozen_string_literal: false
require 'irb'
require 'readline'
require "tempfile"

require_relative "helper"

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
Expand Down