ruby

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

Solution

  • 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.