Ruby Tuesday – File::exist?

Today’s Ruby Tuesday is on File::exist?.

There are times when you want to know if a given file or directory exists; usually this is captured from some kind of configuration file, and you want to make sure the item exists before you want to try to process that path so you can give a nice error. Enter File::exist?.

File::exist? will check to see if the file or directory passed as an argument can be seen by Ruby.

File.exist? "foo"
# => false
File.exist? "junk"
# => true
File.exist? "/usr/local/bin"
# => true
File.exist? "/usr/local/junk"
# => false
File.exist? "."
# => true
File.exist? ".."
# => true

–Proctor