Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

undefined method `exists?' for File:Class (NoMethodError)?

Tags:

ruby

   3.2.0 :002 > File.exists?("xyz")
(irb):2:in `<main>': undefined method `exists?' for File:Class (NoMethodError)
Did you mean?  exist?                             
        from /Users/jason/.rvm/rubies/ruby-3.2.0/lib/ruby/gems/3.2.0/gems/irb-1.6.2/exe/irb:11:in `<top (required)>'
        from /Users/jason/.rvm/rubies/ruby-3.2.0/bin/irb:25:in `load'
        from /Users/jason/.rvm/rubies/ruby-3.2.0/bin/irb:25:in `<main>'
like image 889
Klortho Avatar asked Sep 06 '25 14:09

Klortho


2 Answers

Starting in Ruby 3.2.0 the exists? (with an S) alias for exist? seems to has been removed.

With Ruby 3.2.0, be sure to use the no-S exist?

% rvm use 3.1.3
Using /Users/jason/.rvm/gems/ruby-3.1.3
[email protected] /Users/jason/Work/Hot_Glue/Example Apps/AltLookup1 [main]
% irb  
3.1.3 :001 > File.exist?("xyz")
 => false 
3.1.3 :002 > File.exists?("xyz")
 => false 
3.1.3 :003 > exit
[email protected] /Users/jason/Work/Hot_Glue/Example Apps/AltLookup1 [main]
% rvm use 3.2.0
Using /Users/jason/.rvm/gems/ruby-3.2.0
[email protected] /Users/jason/Work/Hot_Glue/Example Apps/AltLookup1 [main]
% irb
3.2.0 :001 > File.exist?("xyz")
 => false 
3.2.0 :002 > File.exists?("xyz")
(irb):2:in `<main>': undefined method `exists?' for File:Class (NoMethodError)
Did you mean?  exist?                             
        from /Users/jason/.rvm/rubies/ruby-3.2.0/lib/ruby/gems/3.2.0/gems/irb-1.6.2/exe/irb:11:in `<top (required)>'
        from /Users/jason/.rvm/rubies/ruby-3.2.0/bin/irb:25:in `load'
        from /Users/jason/.rvm/rubies/ruby-3.2.0/bin/irb:25:in `<main>'
like image 55
Jason FB Avatar answered Sep 09 '25 11:09

Jason FB


You forgot the question mark (?) at the end:

File.exist? 'foo'
File.exists? 'foo'

In general, methods which answer questions will always end with a question mark.

In this case, the method is asking File the does 'foo' exist? question. The class will return the answer.

like image 34
Matheus Moreira Avatar answered Sep 09 '25 11:09

Matheus Moreira