69
58

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

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”
69
58
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
69
58

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?