End of Life Ruby 3.1 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
instance method
Pathname#sub_ext
sub_ext(p1)
ext/pathname/pathname.c
View on GitHub ↗
static VALUE
path_sub_ext(VALUE self, VALUE repl)
{
VALUE str = get_strpath(self);
VALUE str2;
long extlen;
const char *ext;
const char *p;
StringValue(repl);
p = RSTRING_PTR(str);
extlen = RSTRING_LEN(str);
ext = ruby_enc_find_extname(p, &extlen, rb_enc_get(str));
if (ext == NULL) {
ext = p + RSTRING_LEN(str);
}
else if (extlen <= 1) {
ext += extlen;
}
str2 = rb_str_subseq(str, 0, ext-p);
rb_str_append(str2, repl);
return rb_class_new_instance(1, &str2, rb_obj_class(self));
}
Return a pathname with repl added as a suffix to the basename.
If self has no extension part, repl is appended.
Example
Pathname.new('/usr/bin/shutdown').sub_ext('.rb') #=> #<Pathname:/usr/bin/shutdown.rb>