Tbpgr Blog

Employee Experience Engineer tbpgr(てぃーびー) のブログ

Ruby | Dir.exist?

概要

Dir.exist?

詳細

Dir.exist? は指定されたディレクトリが存在するか確認します。
エイリアスとして exist? があります。
処理結果は

File.directory?

と同じです。

サンプル

# encoding: utf-8
require "pp"

Dir.mkdir "hoge" unless File.exists? "hoge"
Dir.chdir "hoge"
Dir.mkdir "hige" unless File.exists? "hige"
`echo hige > hige1.txt`
`echo hige > hige2.txt`
Dir.chdir "../"
dirs = ["./hoge", "./hoge/hige", "./hage"]
dirs.each do |d|
  puts "#{d} exists?"
  puts Dir.exist? d
  puts Dir.exists? d
  puts File.directory? d
end

出力

./hoge exists?
true
true
true
./hoge/hige exists?
true
true
true
./hage exists?
false
false
false