dirname(p1)
public
Returns all components of the filename given in file_name except
the last one. The filename must be formed using forward slashes (“/”)
regardless of the separator used on the local file system.
File.dirname("/home/gumby/work/ruby.rb")
Show source
static VALUE
rb_file_s_dirname(VALUE klass, VALUE fname)
{
const char *name, *root, *p;
VALUE dirname;
FilePathStringValue(fname);
name = StringValueCStr(fname);
root = skiproot(name);
if (root > name + 1 && isdirsep(*name))
root = skipprefix(name = root - 2);
if (root > name + 1)
name = root - 1;
p = strrdirsep(root);
if (!p) {
p = root;
}
if (p == name)
return rb_usascii_str_new2(".");
if (has_drive_letter(name) && isdirsep(*(name + 2))) {
const char *top = skiproot(name + 2);
dirname = rb_str_new(name, 3);
rb_str_cat(dirname, top, p - top);
}
else
dirname = rb_str_new(name, p - name);
if (has_drive_letter(name) && root == name + 2 && p - name == 2)
rb_str_cat(dirname, ".", 1);
rb_enc_copy(dirname, fname);
OBJ_INFECT(dirname, fname);
return dirname;
}