views:

157

answers:

2

Hi I have been testing some very basic things in ruby and discover the following.

If i put in a file called xxxx.rb in this path "C:\Documents and Settings\Desktop\xxxx.rb"

puts __FILE__

and invoke this ruby file in a command line WITHOUT preceding ruby the output is the following

C:/Documents and Settings/Desktop/xxxx.rb

but if i invoke the xxxx.rb file with ruby (ruby xxxx.rb) in the command like the output is the following:

xxxx.rb

Why is that difference?? Thanks

PD: I'M ON WINDOWS XP SP3

RUBY VERSION: 1.8.6

+2  A: 

I'm guessing that when you just double click on the file, the absolute path gets passed. You should achieve the same effect by calling it like:

ruby C:/Documents and Settings/Desktop/xxxx.rb

Geo
Yes, you are right. But i'm not double clicking the .rb file. I'm just calling the file in the cmd line prompt in two ways. The first way only calling xxxx.rb (obviousley the path were i am in is C:/Documents and Settings/Desktop/)The second way calling ruby xxxx.rbI think there shouldn'b be differences but well, calling the file like yours answers bring me the absolute path...I am still confused though...Thanks.
juanmaflyer
+1  A: 

What you want is to expand the path properly:

# Affected by the current working directory, etc.
puts __FILE__

# Always an absolute path
puts File.expand_path(__FILE__, Dir.getwd)

This takes your current working directory into account.

tadman
Thanks for that appreciation. I find it useful, however i'm still confused about the fact of getting two different outputs just calling the file whit the subtle difference of adding ruby in front of xxxx.rbIf a call the file in this ways:Way 1: xxxx.rbWay 2: ruby xxxx.rbI expect to obtain the same result but it give me different results...Thanks a lot.
juanmaflyer