diff --git a/recipes/sandbox/20-extend-irb.rake b/recipes/sandbox/20-extend-irb.rake index 0a6b13e66..1255e4bba 100644 --- a/recipes/sandbox/20-extend-irb.rake +++ b/recipes/sandbox/20-extend-irb.rake @@ -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", }) diff --git a/resources/files/irbrc_predefiner.rb b/resources/files/irbrc_predefiner.rb index 3c8bc3c5b..6f603b801 100644 --- a/resources/files/irbrc_predefiner.rb +++ b/resources/files/irbrc_predefiner.rb @@ -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