From: Ferenc Engard Date: 2004-05-28T12:05:06+09:00 Subject: Re: absolute location of a script file? 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