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

() -> ::Pathname
dirname → new_pathname

Returns a new pathname whose path is all but the last component of the path in self:

Example
Pathname('/usr/lib/linux').basename # => #<Pathname:linux>
Pathname('/usr').basename           # => #<Pathname:usr>
Pathname('/').basename              # => #<Pathname:/>
Pathname('lib/').basename           # => #<Pathname:lib>
Pathname('nosuch').basename         # => #<Pathname:nosuch>

Components are delimited by File::SEPARATOR and, if non-nil, File::ALT_SEPARATOR.

See also #directory? #each_child #descend #each_entry also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0