Skip to content

End of Life Ruby 3.0 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

class method

File.dirname

File.dirname(file_name) → dir_name

Returns all components of the filename given in file_name except the last one (after first stripping trailing separators). The filename can be formed using both File::SEPARATOR and File::ALT_SEPARATOR as the separator when File::ALT_SEPARATOR is not nil.

Example
File.dirname("/home/gumby/work/ruby.rb")   #=> "/home/gumby/work"
See also .directory? .empty? .delete .executable? also in 4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.1