End of Life Ruby 3.1 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
instance method
String#delete_suffix
str.delete_suffix(suffix) → new_str
string.c
View on GitHub ↗
static VALUE
rb_str_delete_suffix(VALUE str, VALUE suffix)
{
long suffixlen;
suffixlen = deleted_suffix_length(str, suffix);
if (suffixlen <= 0) return str_duplicate(rb_cString, str);
return rb_str_subseq(str, 0, RSTRING_LEN(str) - suffixlen);
}
Returns a copy of str with trailing suffix deleted.
Example
"hello".delete_suffix("llo") #=> "he" "hello".delete_suffix("hel") #=> "hello"
Ruby version changes
-
3.0
-
The following methods now return or yield
Stringinstances instead of subclass instances when called on subclass instances: [Bug #10845]
-
See also
#delete_suffix!
#delete_prefix!
#delete_prefix
#downcase
also in
4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.0