64

In my Ruby project I am using a mess of things like moving and editing files on several remote boxes and I really need something like a relative path to my root project directory. I have many processing folders which are used in many methods.

Right now I have paths hardcoded, but that makes me unhappy.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
qwebek
  • 949
  • 2
  • 8
  • 15

3 Answers3

135

You can get current directory (directory of current file) with this

File.dirname(__FILE__)

You can then join it with relative path to the root

File.join(File.dirname(__FILE__), '../../') # add proper number of ..

Or you can use expand_path to convert relative path to absolute.

ENV['BUNDLE_GEMFILE'] = File.expand_path('../../Gemfile', File.dirname(__FILE__))

Or you can calculate relative path between two dirs.

require 'pathname'; 
puts Pathname.new('/').relative_path_from(Pathname.new('/some/child/dir/')).to_s
# => ../../..
Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367
  • 1
    So, what exactly is your problem? – Sergio Tulentsev Feb 23 '12 at 16:50
  • 1
    The certain problem i faced is following - FileUtils.chdir("\\\\someRemoteIp\\somedir") FileUtils.cp(filename, "local_box_drive\\folder") then i need to extract some data from the file which is in the root of project i have to do FileUtils.chdir("E:\\folder\\folder\\my_project_folder") my_method(local_file_in_root_project_folder) If i will run such code on some other box, it will fail, i need FileUtils.chdir("E:\\folder\\folder\\my_project_folder") substituted to some relative expression that will bring me to my project root folder – qwebek Feb 23 '12 at 16:58
  • Do you have project folder in different locations on different boxes? – Sergio Tulentsev Feb 23 '12 at 16:59
  • I need to be able to run the project on several boxes, so there should be no paths mapped to hard drives when making operations with files in project dir. – qwebek Feb 23 '12 at 19:26
  • i need somethuing like RAILS_ROOT or Rails.root – qwebek Mar 01 '12 at 10:51
  • Can't you set that env var in the main script, or upon invocation? Like `SCRIPT_ROOT=/my/dir ruby script.rb`. – Sergio Tulentsev Mar 01 '12 at 11:02
33
__dir__

Since Ruby 2, you can simply use Kernel-function :__dir__ to get the absolute directory-path of the current file. So, to give an example, you could set a constant ROOT_DIR at the start of your project in the (config.rb, environments.rb, constants.rb, or whatever you call it).

See Ruby Documentation

Andreas Rayo Kniep
  • 5,674
  • 2
  • 30
  • 30
  • 1
    This is what I use presently, it's quite short and thus nice. I also keep a constant internally in a project, to keep track of it; and sometimes also a method that will return the path to the base directory, if the user is able to designate and overrule it (e. g. sometimes you may have to do so, such as when you are in a restricted environment like on campus, or when distributions such as debian split up components of ruby by default - debian uses some strange ruby paths). – shevy Jan 16 '19 at 17:01
1
ROOT_DIR = File.expand_path(".")
  • you can call it from any sub-script, it will resolve to project root dir
Daniel Garmoshka
  • 5,849
  • 39
  • 40