ls 相当の操作
グロブに合致するエントリーを返す。
Dir.glob("*") # => ["bar", "foo"]
グロブをドットに合致させる。
Dir.glob("*", File::FNM_DOTMATCH) #=> [".", "..", "bar", "foo"]
"."
と ".."
以外のエントリーを返す。
Dir.children(".") # => ["bar", "foo"]
エントリーを全て返す。"."
と ".."
を含む。
Dir.entries(".") # => [".", "..", "bar", "foo"]
参考:
ディレクトリ内のファイルを列挙する | まくまく Ruby ノート
singleton method Dir.[] (Ruby 2.5.0)
singleton method Dir.children (Ruby 2.5.0)
singleton method Dir.entries (Ruby 2.5.0)
子ディレクトリを取得する
カレントディレクトリの子ディレクトリを取得する
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
参考:
Getting a list of folders in a directory – Stack Overflow
singleton method Dir.children (Ruby 2.5.0)
singleton method File.directory? (Ruby 2.5.0)
Dir.glob
参考:
Ruby で特定のディレクトリ配下で○日以上古いファイルを削除する – Qiita
singleton method Dir.[] (Ruby 2.5.0)
Find.find
参考:
ディレクトリを再帰的に走査する Dir.glob と Find.find | Ruby入門 – Worth Living
module function Find.#find (Ruby 2.5.0)
glob で走査して require する
参考:
Ruby で Dir.glob して require するときは sort してからにする | Born Too Late
ruby でライブラリなどを書くとき、Rakefile と相対パスの require を使ってクラスの取得やテストをやりやすくする – woshidan’s loose leaf