Hi all,
I have a library-like script, which can be used from many apps, from
many directories. I need to get the absolute path of the (required)
script file, so, for example, I can always find a file which is in the
same directory where my library script is. Is there a way to do this?
Thanks,
Ferenc
Ferenc Engard wrote:
Hi all,
I have a library-like script, which can be used from many apps, from
many directories. I need to get the absolute path of the (required)
script file, so, for example, I can always find a file which is in the
same directory where my library script is. Is there a way to do this?
From within the script file itself, you can get it with
File.expand_path(FILE)
So for instance, you might do
abs_path = File.expand_path(FILE)
abs_dir = File.dirname(abs_path)
But that doesn’t help if you access the script using
require ‘my-script’
In that case, you can define a global constant in the the script file
and read that constant from the main program. (If you have to do this
for a number of different scripts and are concerned about conflicts in
the global namespace, see http://ruby-talk.org/100429.)
HTH.
Joel VanderWerf wrote:
Ferenc Engard wrote:
Hi all,
I have a library-like script, which can be used from many apps, from
many directories. I need to get the absolute path of the (required)
script file, so, for example, I can always find a file which is in the
same directory where my library script is. Is there a way to do this?
From within the script file itself, you can get it with
File.expand_path(FILE)
So for instance, you might do
abs_path = File.expand_path(FILE)
abs_dir = File.dirname(abs_path)
But that doesn’t help if you access the script using
require ‘my-script’
Er, why not? I have made some experiments, so it seems that it is always
true that on the start of my (required) scriptfile, the
‘File.expand_path(FILE)’ always returns the absolute path to the
file, at least the following seems to work:
start of my library script, which is included by other files…
module MOD_LIB
SCRIPTDIR=File.split(File.expand_path(FILE))[0]
end
…
After that, I can use MOD_LIB::SCRIPTDIR to find files which are in the
same directory as the libary script itself. Or not?
Thanks for the help:
Ferenc
Ferenc Engard wrote:
Joel VanderWerf wrote:
Ferenc Engard wrote:
Hi all,
I have a library-like script, which can be used from many apps, from
many directories. I need to get the absolute path of the (required)
script file, so, for example, I can always find a file which is in the
same directory where my library script is. Is there a way to do this?
From within the script file itself, you can get it with
File.expand_path(FILE)
So for instance, you might do
abs_path = File.expand_path(FILE)
abs_dir = File.dirname(abs_path)
But that doesn’t help if you access the script using
require ‘my-script’
Er, why not? I have made some experiments, so it seems that it is always
I meant that FILE only gives you the path to the script when
FILE is referenced in the script itself. Sorry for the confusion.
true that on the start of my (required) scriptfile, the
‘File.expand_path(FILE)’ always returns the absolute path to the
file, at least the following seems to work:
start of my library script, which is included by other files…
module MOD_LIB
SCRIPTDIR=File.split(File.expand_path(FILE))[0]
end
…
After that, I can use MOD_LIB::SCRIPTDIR to find files which are in the
same directory as the libary script itself. Or not?
Yeah, that’s basically what I was trying to say 
Thanks for the help:
Y’r welcome!
···
Ferenc