Question

How do I get the directory where the rakefile.rb is located?

I want to use this as my root directory to locate everything off.

Was it helpful?

Solution

use __FILE__ to get the file name then you can get the directory from there:

in test.rb

puts __FILE__

output:

/users/foo/test.rb

__FILE__ resolves to the full path of the file it is in.

Use this to get the dir name:

File.dirname(__FILE__)

OTHER TIPS

You can get it by calling application.original_dir method. In task you can achieve application object using application method on task object.

Why not just use Dir.pwd

?

If this is a RoR app your Rakefile.rb should be in your RAILS_ROOT directory. So in any script you can specify file location like

 config.load_paths += %W( #{RAILS_ROOT}/extras )

As of Ruby 2 you can use __dir__ instead of File.dirname(__FILE__) to get the directory that contains the current script.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top