Maintenance Ruby 3.4 gets fixes, but no new features. Switch to Ruby 4.0 →
class method
File.dirname
(::string | ::_ToPath file_name, ?::Integer level) -> ::String
File.dirname(file_name, level = 1) → dir_name
file.c
View on GitHub ↗
static VALUE
rb_file_s_dirname(int argc, VALUE *argv, VALUE klass)
{
int n = 1;
if ((argc = rb_check_arity(argc, 1, 2)) > 1) {
n = NUM2INT(argv[1]);
}
return rb_file_dirname_n(argv[0], n);
}
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"
If level is given, removes the last level components, not only one.
Example
File.dirname("/home/gumby/work/ruby.rb", 2) #=> "/home/gumby" File.dirname("/home/gumby/work/ruby.rb", 4) #=> "/"
Ruby version changes
-
3.1
-
File.dirnamenow accepts an optional argument for the level to strip path components. [Feature #12194]
-