Getting path of current script

  • Thread starter Joe Ruby MUDCRAP-CE
  • Start date
J

Joe Ruby MUDCRAP-CE

Is there a simpler way to get the absolute path of the current script?

path = File.dirname(File.expand_path(__FILE__))

File.dirname(__FILE__) by itself can return a relative or absolute path.

Joe
 
A

ara.t.howard

Is there a simpler way to get the absolute path of the current script?

path = File.dirname(File.expand_path(__FILE__))

File.dirname(__FILE__) by itself can return a relative or absolute path.

Joe

just a note, what you have above is not the dir for the current script, it's
the dir for the current file - eg the value will be the same whether the file
is run or required/loaded. for the current script you need

path = File.dirname(File.expand_path($0))

or

File.dirname($0)

-a
 
N

Nate Wiger

just a note, what you have above is not the dir for the
current script, it's the dir for the current file - eg the
value will be the same whether the file is run or required/loaded.
for the current script you need

path = File.dirname(File.expand_path($0))

or

File.dirname($0)

I think the main problem is that the second solution is not an abs path
if you type "./script" or even "../../bin/script".

Your first suggestion is what I would use personally.

-Nate
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,979
Messages
2,570,185
Members
46,727
Latest member
FelicaTole

Latest Threads

Top