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
file.c
View on GitHub ↗
static VALUE
s_absolute_path(int c, const VALUE * v, VALUE _)
{
return rb_file_s_absolute_path(c, v);
}
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"