Skip to content

Commit

Permalink
Remove .irbrc file on rubies >= 3.3
Browse files Browse the repository at this point in the history
It is no longer necessary, since the enabled extensions have been made defaults in ruby core.
So ruby-3.3 uses the irb_predefiner to cleanup a previously generated .irbrc from the users home directory, so that the error output due to changed require paths is avoided.
Previously the outdated .irbrc led to an error like so:

$ irb
Error loading RC file 'C:/Users/Administrator/.irbrc':
<internal:C:/Ruby33-x64/lib/ruby/3.3.0/rubygems/core_ext/kernel_require.rb>:127:in `require': cannot load such file -- irb/ext/save-history (LoadError)
Did you mean?  irb/ext/eval_history
        from <internal:C:/Ruby33-x64/lib/ruby/3.3.0/rubygems/core_ext/kernel_require.rb>:127:in `require'
        from C:/Users/Administrator/.irbrc:1:in `<top (required)>'
        from C:/Ruby33-x64/lib/ruby/3.3.0/irb/init.rb:399:in `load'
  • Loading branch information
larskanis committed Dec 25, 2023
1 parent f741ee5 commit f13e3b8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
4 changes: 4 additions & 0 deletions recipes/sandbox/20-extend-irb.rake
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# This adds some logic to define a .irbrc to enable history saving and completion.
# These features are enabled by default since ruby-3.3 and require paths have been changed.
# Therefore the job on later versions is inverted to remove the .irbrc when unchanged.

self.import_files.merge!({
"resources/files/irbrc_predefiner.rb" => "lib/ruby/site_ruby/#{package.rubylibver}/irbrc_predefiner.rb",
})
Expand Down
32 changes: 21 additions & 11 deletions resources/files/irbrc_predefiner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,30 @@

irbrc_file = IRB.enum_for(:rc_file_generators).first.call(IRB::IRBRC_EXT)

if irbrc_file && !File.exist?(irbrc_file) && File.exist?(File.dirname(irbrc_file))
File.write irbrc_file, <<-EOT
irbrc_text = <<-EOT
require 'irb/ext/save-history'
require 'irb/completion'
IRB.conf[:SAVE_HISTORY] = 200
EOT
end
EOT

if RUBY_VERSION < "3.3"
# Create a .irbrc file with default options on older rubies
if irbrc_file && !File.exist?(irbrc_file) && File.exist?(File.dirname(irbrc_file))
File.write irbrc_file, irbrc_text
end

# Try to convert .irb_history from locale to UTF-8, if it isn't encoded properly.
# This is for transition from CP* encodings of RbReadline to UTF-8 of Reline.
history_file = IRB.rc_file("_history")
if File.exist?(history_file) && !(hist=File.read(history_file, encoding: 'utf-8')).valid_encoding?
hist = hist.encode('utf-8', Encoding.find("locale"))
if hist.valid_encoding?
File.write(history_file, hist)
# Try to convert .irb_history from locale to UTF-8, if it isn't encoded properly.
# This is for transition from CP* encodings of RbReadline to UTF-8 of Reline.
history_file = IRB.rc_file("_history")
if File.exist?(history_file) && !(hist=File.read(history_file, encoding: 'utf-8')).valid_encoding?
hist = hist.encode('utf-8', Encoding.find("locale"))
if hist.valid_encoding?
File.write(history_file, hist)
end
end
else
# Remove the now unnecessary .irbrc file when unchanged on newer rubies
if irbrc_file && File.exist?(irbrc_file) && File.read(irbrc_file) == irbrc_text
File.unlink irbrc_file
end
end

0 comments on commit f13e3b8

Please sign in to comment.