Skip to content

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

instance method

Pathname#realdirpath

(?::string | ::Pathname base_dir) -> ::Pathname
realdirpath → new_pathname

Returns a new pathname containing the real (absolute) pathname of the path in self; the new path is the path in the actual filesystem, and does not contain useless dot-entries ('.' or '..') or symbolic links:

Example
Pathname('/etc/./passwd/../../var').realdirpath
# => #<Pathname:/var>

Only the last component of the new path may be nonexistent:

Example
Pathname('/etc/./passwd/../../var/nosuch').realdirpath
# => #<Pathname:/var/nosuch>
Pathname('/etc/./passwd/../../var/nosuch/nosuch').realdirpath
# Raises Errno::ENOENT: No such file or directory.

Method realpath is similar, but requires all components to exist.

See also #readlink #realpath #readlines #relative? also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0