ディレクトリ・ファイルの有無確認方法 †ファイルまたはディレクトリが存在するかしないかを確認するサンプルコードを以下に記します。 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が返却されます。 スポンサーリンク |