method

expand_path

v1_8_7_330 - Show latest stable - Class: File
expand_path(...)
public

Converts a pathname to an absolute pathname. Relative paths are referenced from the current working directory of the process unless dir_string is given, in which case it will be used as the starting point. The given pathname may start with a “~”, which expands to the process owner’s home directory (the environment variable HOME must be set correctly). “~user” expands to the named user’s home directory.

File.expand_path("~oracle/bin")           #=> "/home/oracle/bin"
File.expand_path("../../bin", "/tmp/x")   #=> "/bin"

3Notes

Argument Ordering

tadman · Apr 23, 20096 thanks

Be aware that the order of arguments for this method is the opposite of File.join:

File.expand_path('foo', '/bar')   # => "/bar/foo"
File.join('foo', '/bar')          # => "foo/bar"

Require file from the same folder

avakhov · Apr 16, 20105 thanks

If you want to require file from the same folder, the simplest way is

require File.expand_path('../file-to-require', __FILE__)

If your file is /lib/book.rb

File.expand_path('../page', '/lib/book.rb') => '/lib/page.rb'

Anothery way

taimoorchangaiz · Sep 25, 2013

==== This Worked For Me

require File.expand_path('../app/models/extenstions/active_record_ext', File.dirname(FILE))

I did this in application.rb