Klortho
Klortho

Reputation: 823

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

   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>'

Upvotes: 26

Views: 30180

Answers (2)

Jason FB
Jason FB

Reputation: 5940

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

With Ruby 3.2.0, be sure to use the singular-form 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>'

Upvotes: 68

Matheus Moreira
Matheus Moreira

Reputation: 17030

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.

Upvotes: 11

Related Questions