Skip to content

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

class method

File.dirname

(::path file_name, ?::Integer level) -> ::String
File.dirname(path, count = 1) → string

Returns a string path containing all but the last count components of the given path:

Example
File.dirname('/usr/lib/linux')     # => "/usr/lib"
File.dirname('/usr')               # => "/"
File.dirname('/')                  # => "/"
File.dirname('lib/')               # => "."
File.dirname('nosuch')             # => "."
File.dirname('/usr/lib/linux', 2)  # => "/usr"
File.dirname('/usr/lib/linux', 20) # => "/"
File.dirname('/usr/lib/linux', 0)  # => "/usr/lib/linux"

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

Ruby version changes

  1. 3.1
See also .directory? .empty? .delete .executable? also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0