Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
Pathname#cleanpath
# File tmp/rubies/ruby-4.1.0-preview1/pathname_builtin.rb, line 629
def cleanpath(consider_symlink=false)
if consider_symlink
cleanpath_conservative
else
cleanpath_aggressive
end
end
Returns a new Pathname object, “cleaned” of unnecessary separators, single-dot entries, and double-dot entries.
When self is empty, returns a pathname with a single-dot entry:
Pathname('').cleanpath # => #<Pathname:.>
Separators
A lone separator is preserved:
Pathname('/').cleanpath # => #<Pathname:/>
Multiple trailing separators are removed:
Pathname('foo/////').cleanpath # => #<Pathname:foo> Pathname('foo/').cleanpath # => #<Pathname:foo>
Multiple embedded separators are reduced to a single separator:
Pathname('foo///bar').cleanpath # => #<Pathname:foo/bar>
Multiple leading separators are reduced:
# On Windows, where File.dirname('//') == '//'. Pathname('/////foo').cleanpath # => #<Pathname://foo> Pathname('/////').cleanpath # => #<Pathname://> # Otherwise, where File.dirname('//') == '/'. Pathname('/////foo').cleanpath # => #<Pathname:/foo> Pathname('/////').cleanpath # => #<Pathname:/>
Single-Dot Entries
A lone single-dot entry is preserved:
Pathname('.').cleanpath # => #<Pathname:.>
A non-lone single-dot entry, regardless of its location, is removed:
Pathname('foo/././././bar').cleanpath # => #<Pathname:foo/bar> Pathname('./foo/./././bar').cleanpath # => #<Pathname:foo/bar> Pathname('foo/./././bar/./').cleanpath # => #<Pathname:foo/bar>
Double-Dot Entries
A lone double-dot entry is preserved:
Pathname('..').cleanpath # => #<Pathname:..>
When a non-lone double-dot entry is preceded by a named entry, both are removed:
Pathname('foo/..').cleanpath # => #<Pathname:.> Pathname('foo/../bar').cleanpath # => #<Pathname:bar> Pathname('foo/../bar/..').cleanpath # => #<Pathname:.> Pathname('foo/bar/./../..').cleanpath # => #<Pathname:.>
When a non-lone double-dot entry is not preceded by a named entry, it is preserved:
Pathname('../..').cleanpath # => #<Pathname:../..>
A non-lone meaningless double-dot entry is removed:
Pathname('/..').cleanpath # => #<Pathname:/> Pathname('/../..').cleanpath # => #<Pathname:/>
Symbolic Links
If the path may contain symbolic links, consider give optional argument symlinks as true; the method then uses a more conservative algorithm that avoids breaking symbolic links. This may preserve more double-dot entries than are absolutely necessary, but without accessing the filesystem, this can’t be avoided.
Examples:
Pathname('a/').cleanpath # => #<Pathname:a> Pathname('a/').cleanpath(true) # => #<Pathname:a/> Pathname('a/.').cleanpath # => #<Pathname:a> Pathname('a/.').cleanpath(true) # => #<Pathname:a/.> Pathname('a/./').cleanpath # => #<Pathname:a> Pathname('a/./').cleanpath(true) # => #<Pathname:a/.> Pathname('a/b/.').cleanpath # => #<Pathname:a/b> Pathname('a/b/.').cleanpath(true) # => #<Pathname:a/b/.> Pathname('a/../.').cleanpath # => #<Pathname:.> Pathname('a/../.').cleanpath(true) # => #<Pathname:a/..> Pathname('a/b/../../../../c/../d').cleanpath # => #<Pathname:../../d> Pathname('a/b/../../../../c/../d').cleanpath(true) # => #<Pathname:a/b/../../../../c/../d>