search
LoginSignup
58

More than 3 years have passed since last update.

posted at

updated at

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

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

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

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
What you can do with signing up
58