File.exists? and Dir.exists? on Ruby 3.2 Workaround
If you try to upgrade to Ruby 3.2 you might see this error:
irb(main):001:0> File.exists?
(irb):1:in `<main>': undefined method `exists?' for File:Class (NoMethodError)
Did you mean? exist?
from C:/Ruby32-x64/lib/ruby/gems/3.2.0/gems/irb-1.6.2/exe/irb:11:in `<top (required)>'
from C:/Ruby32-x64/bin/irb:25:in `load'
from C:/Ruby32-x64/bin/irb:25:in `<main>'
Or the one for Dir.exists?
(irb):3:in `<main>': undefined method `exists?' for Dir:Class (NoMethodError)
Did you mean? exist?
from C:/Ruby32-x64/lib/ruby/gems/3.2.0/gems/irb-1.6.2/exe/irb:11:in `<top (required)>'
from C:/Ruby32-x64/bin/irb:25:in `load'
from C:/Ruby32-x64/bin/irb:25:in `<main>'
The best way is if you just use File.exist? and Dir.exist? instead of File.exists? and Dir.exists?, because those were deprecated since at least Ruby 2.3 and removed in Ruby 3.2
Often, these methods were used in gems that cannot easily be updated. Luckily we use Ruby and so there is a simple workaround, which I put into a Gem.
How to monkeypatch:
class File
def self.exists?(filename)
self.exist?
end
end
This needs to run before any code that uses the File.exists method. To make it simpler I packaged it into the file_exists gem.
Just
gem install file_exists
and include it with
require 'file_exists'
before all the other code.