Search code examples
ruby-on-railsrubyfile-exists

Ruby keeps telling me my file does not exist


I'm trying to check if a file exists with RoR but it keeps saying this file does not exist and I can't understand why.

if File.exist?('~/Desktop/test.xls')
  p 'File EXISTS'
else
  p 'Cannot find any file'
end

Of course the file exists and I can access it with the terminal using 'ls'.

The script is into the scripts folder of my rails app.

Thanks for any help


Solution

  • It should be something like this:

    if File.exist?("#{Dir.home}/Desktop/test.xls")
      p 'File EXISTS'
    else
      p 'Cannot find any file'
    end
    

    where Dir.home is your particular home directory (in my case it is /Users/m.pontyushenko/)