File.expand_path(__FILE__)

I’m having a problem with File.expand_path(FILE) after a chdir.
Looks like a bug in Ruby to me. If someone can tell me what I’m doing
wrong, I’d be grateful.

Here’s the reduction:

[ryan@kursk 15:26:38 ~]$ cat tmp/demo-problem.rb
puts "starting working directory: " + Dir.getwd
puts "expand_path: " + File.expand_path(FILE)
puts "about to change to dirname of FILE: " + FILE
Dir.chdir(File.dirname(FILE))
puts "new working directory: " + Dir.getwd
puts "expand_path: " + File.expand_path(FILE)

IIUC, It’s actually behaving properly: Most dir references are relative
by default in ruby. Thus, FILE in your example is just
‘temp/demo.rb’ and not the absolute path.

Add a call to Dir.getwd at the end and you’ll see you are in the right
path. I usually define at the start of my apps a constant:
$APP_PATH = File.expand_path(File.dirname(FILE))

Usually closely followed by:
$LOAD_PATH << $APP_PATH