FILE は、そのままだとアテにならない。
FILE
現在のソースファイル名
フルパスとは限らないため、フルパスが必要な場合は File.expand_path( FILE) とする必要があります。
フルパスとは限らないというか、シンボリックリンクでも元ファイルにはならない。ソースファイル名というより、「実行中のファイル名」ですね。
たとえば、リンクを作ってみる。
ln -s sample.rb ~/linked.rb
sample.rb を linked.rb にして、リンクした場合の FILE がどうなるか調べてみることにしたら・・・
~/Desktop/sample.rb
__FILE__: sample.rb $0 : sample.rb File.expand_path($0): /Users/takuya/Desktop/sample.rb File.realpath($0) : /Users/takuya/Desktop/sample.rb File.expand_path(__FILE__): /Users/takuya/Desktop/sample.rb File.realpath(__FILE__) : /Users/takuya/Desktop/sample.rb
~/linked.rb
__FILE__: /Users/takuya/linked.rb $0 : /Users/takuya/linked.rb File.expand_path($0):/Users/takuya/linked.rb # これは・・・ File.realpath($0) : /Users/takuya/Desktop/sample.rb File.expand_path(__FILE__): /Users/takuya/linked.rb # あっ File.realpath(__FILE__) :/Users/takuya/Desktop/sample.rb
FILE は実行中のファイル
リンクしたりアレコレ、 FILE はいまのPathなんだろうけど、require したらどうなるんだろう
Pathが必要ならrealpath した方がいいね
$: / $LOAD_PATH や、なにかファイルを読み込むときに、実行中のファイル名から相対的にたどるのであれば、expand_pathするまえに、realpathしないとバグが起きそう。。。