ファイルまたはディレクトリが存在するかしないかを確認するサンプルコードを以下に記します。
有無確認は以下の構文で簡単に調べることができます。
File.exist?(ファイル名orディレクトリ名)
以下にサンプルコードを記します。
# check directory p File.exist?("/tmp") p File.exist?("/hoge") # check file p File.exist?("/etc/hosts") p File.exist?("/etc/hoge")
$ ruby file_exist.rb true false true false
存在する場合はtrue、しない場合はfalseが返却されます。