Skip to content

Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →

class method

File.absolute_path

(::path file_name, ?::path dir_string) -> ::String
File.absolute_path(path, dirpath = '.') → absolute_path

Returns the string absolute path for the given path.

Evaluates a relative path with respect to the directory given by dirpath:

Example
Dir.chdir('/snap')
# Default dirpath.
File.absolute_path('README')                  # => "/snap/README"
File.absolute_path('bin')                     # => "/snap/bin"
File.absolute_path('bin/../var')              # => "/snap/var"
# Other dirpath.
File.absolute_path('../zip', '/usr/bin/ruby') # => "/usr/bin/zip"

For an absolute path, argument dirpath is ignored:

Example
File.absolute_path('/snap', '/usr/bin')       # => "/snap"
File.absolute_path('/snap', 'nosuch')         # => "/snap"

A leading tilde character ('~'), is not expanded:

Example
Dir.chdir('/usr/bin')
File.absolute_path("~")                       # => "/usr/bin/~"
File.absolute_path("~/Documents")             # => "/usr/bin/~/Documents"
See also .absolute_path? .atime also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0