LoginSignup
58

More than 5 years have passed since last update.

Railsのファイルパスの操作のメモ

Last updated at Posted at 2016-12-08

毎回ググっているのでメモ

プロジェクトルートパスの取得

Rails.root.to_s
# => "/Users/user_name/Documents/workspace/ruby/project_name"

現在の実行ファイルのパス

__FILE__
# => "/Users/user_name/Documents/workspace/ruby/project_name/spec/exec_file.rb"

ディレクトリの取得

Rails.root.dirname
# => "/Users/user_name/Documents/workspace/ruby"
File.dirname(__FILE__)
# => "/Users/user_name/Documents/workspace/ruby/project_name/spec”

パスの加工

Rails.root.join('hoge', 'huga').to_s
# =>"/Users/user_name/Documents/workspace/ruby/project_name/hoge/fuga"
File.expand_path('../../shared/log/unicorn.log', Rails.root)
# => "/Users/user_name/Documents/workspace/shared/log/unicorn.log”

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
58