Dir
参考:
class Dir | Ruby リファレンスマニュアル [公式]
使い方
グロブに合致するエントリーを返す。
Dir.glob("*") # => ["bar", "foo"]
グロブをドットに合致させる。
Dir.glob("*", File::FNM_DOTMATCH) #=> [".", "..", "bar", "foo"]
"." と ".." 以外のエントリーを返す。
Dir.children(".") # => ["bar", "foo"]
エントリーを全て返す。. (カレントディレクトリ) と .. (親ディレクトリ) を含む。
Dir.entries(".") # => [".", "..", "bar", "foo"]
参考:
Dir.[] | Ruby リファレンスマニュアル [公式]
Dir.children | Ruby リファレンスマニュアル [公式]
Dir.entries | Ruby リファレンスマニュアル [公式]
子ディレクトリを取得する
カレントディレクトリの子ディレクトリを取得する。
Dir.children(".").select do |entry|
File.directory?(
File.expand_path(entry)
)
end
指定したディレクトリの子ディレクトリを取得する。
dir_path = "path/to/directory"
Dir.children(dir_path).select do |entry|
File.directory?(
File.expand_path(entry, dir_path)
)
end
参考:
Dir.children | Ruby リファレンスマニュアル [公式]
File.directory? | Ruby リファレンスマニュアル [公式]
Getting a list of folders in a directory – Stack Overflow
Dir.glob
参考:
Ruby で特定のディレクトリ配下で○日以上古いファイルを削除する – Qiita
Dir.[] | Ruby リファレンスマニュアル [公式]
Find.find
参考:
ディレクトリを再帰的に走査する Dir.glob と Find.find | Worth Living
Find.#find | Ruby リファレンスマニュアル [公式]
ライブラリをまとめて require する
参考:
Ruby で Dir.glob して require するときは sort してからにする | Born Too Late
Ruby でライブラリなどを書くとき、Rakefile と相対パスの require を使ってクラスの取得やテストをやりやすくする | woshidan’s loose leaf