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#realpath

(?::string | ::Pathname base_dir) -> ::Pathname
realpath → 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').realpath
# => #<Pathname:/var>

All components of the new path must exist:

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

Method realdirpath is similar, but does not require the last component to exist.