28
22

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.

Pathnameってなんじゃらほい

Last updated at Posted at 2013-04-18
pathname_example.rb
Rails.root
# #<Pathname:/path/to/rails/>

Rails.root.join('hoge')
# #<Pathname:/path/to/rails/hoge>

Rails.root.join('hoge').join('unko')
# #<Pathname:/path/to/rails/hoge/unko>

Rails.root.join('hoge').join('unko').to_s
# "/path/to/rails/hoge/unko"

いちいち、root + '/' + 'app' + '/' + 'filename' のようにディレクトリ階層を文字列的に繋げずにオブジェクト指向的に書くことができる。

Railsではそのまま引数に与えれば勝手にto_sしてくれるメソッドも多い。なるほど便利だ。PHPにも欲しいな。

Railsの機能じゃなくて、Rubyの標準ライブラリだったか。
http://doc.ruby-lang.org/ja/1.9.3/class/Pathname.html

root + 'app' + 'filename' のように+で連結もできる。
join()メソッドは複数階層を一度に指定したい場合に有効とのこと。

28
22
0

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
28
22

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?