Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
instance method
Pathname#chop_basename
(untyped path) -> untyped
chop_basename(p1)
pathname.c
View on GitHub ↗
static VALUE
chop_basename(VALUE self, VALUE path)
{
rb_len_t baselen, alllen = RSTRING_LEN(check_strpath(path));
if (alllen <= 0) return Qnil;
rb_encoding *enc = rb_enc_get(path);
const char *name = RSTRING_PTR(path);
const char *base = ruby_enc_find_basename(name, &baselen, &alllen, enc);
if (baselen < 1) return Qnil;
if (baselen == 1 && isdirsep(*base)) return Qnil;
RUBY_ASSERT(base >= name);
RUBY_ASSERT(base <= RSTRING_END(path));
VALUE dir = rb_str_subseq(path, 0, base - name);
VALUE basename = rb_enc_str_new(base, alllen, enc);
RB_GC_GUARD(path);
return rb_assoc_new(dir, basename);
}
chop_basename(path) -> [pre-basename, basename] or nil