How to check whether a File exists

musicmatze picture musicmatze · Sep 30, 2015 · Viewed 11.4k times · Source

I have an Array of Strings and I want to select only these Strings which are paths to files:

My path is "~/dlds/some_file.ics" where ~/dlds is a symlink to ~/archive/downloads on my system. The file has following permissions:

-rw-r--r--

My code (I tried several variants):

ARGV.select do |string|
    File.file? string # returns false
    Pathname.new(string).file? # returns false
    Pathname.new(string).expand_path.file? # returns false
end

I don't know what else to try.

I'm running Ruby 2.2.0 or 2.2.2.

Answer

Aleksei Matiushkin picture Aleksei Matiushkin · Sep 30, 2015
File.exist? File.expand_path "~/dlds/some_file.ics"