LoginSignup
5
4

More than 5 years have passed since last update.

Railsの特定のディレクトリ以下のファイルを探る

Posted at

再帰的に特定のディレクトリ以下のファイルパスを取得したい時は、
Dirクラスを使ってパスを取得する。

Railsで呼び出す場合は、Rails.rootを使うと実行しているOSでRailsが動いている絶対パスを取得できるので、本番環境や開発環境を指揮する必要がなくて便利

実行例

Dir.glob("#{Rails.root}/app/models/**/*") do |f|
  p f
end

=> "/var/app/models/user.rb"
"/var/app/models/owner.rb"
"/var/app/models/blog"
"/var/app/models/blogs/comment.rb"
5
4
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4