Locate rails application root directory

Is there a simple way to obtain the full path of the Rails application
root directory from the Rails application?
(The parent dir of the apps,public, etc. directories)

Thanks in advance,
Don McClean

I’m relatively new to Unix. Is there a problem with using the relative
path
vs. the absolute? I’d just like to be clearer on that 'cause I sometimes
see
just RAILS_ROOT and sometimes the File.expand_path. I’m no fan of the
cargo
cult so I’ve just stuck with using RAILS_ROOT. :wink:

RSL

Is there a simple way to obtain the full path of the Rails application
root directory from the Rails application?
(The parent dir of the apps,public, etc. directories)

The RAILS_ROOT constant.

However this will most likely be a relative path, but you can use
File.expand_path to make it absolute.

-philip

There can be a problem with relative vs absolute path when it comes to
#require:

require ‘./…/lib/file’
vs
require ‘/home/user/site/lib/file’

Doing both will actually require the file twice, as Ruby caches
according to
the string given. While this is a rare case, it can lead to a lot of
debugging time if you don’t realize this, so be careful.

Jason

Thanks!

I’m relatively new to Unix. Is there a problem with using the relative path
vs. the absolute? I’d just like to be clearer on that 'cause I sometimes see
just RAILS_ROOT and sometimes the File.expand_path. I’m no fan of the cargo
cult so I’ve just stuck with using RAILS_ROOT. :wink:

I don’t think so… maybe in some cases where you have some shared code
and want to test the path to set a variable… of some sort… in that
case you’d want the full path to test with.

Thanks!

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs