Maintenance Ruby 3.4 gets fixes, but no new features. Switch to Ruby 4.0 →
instance method
String#end_with?
(*::string suffixes) -> bool
end_with?(*strings) → true or false
string.c
View on GitHub ↗
static VALUE
rb_str_end_with(int argc, VALUE *argv, VALUE str)
{
int i;
for (i=0; i<argc; i++) {
VALUE tmp = argv[i];
const char *p, *s, *e;
long slen, tlen;
rb_encoding *enc;
StringValue(tmp);
enc = rb_enc_check(str, tmp);
if ((tlen = RSTRING_LEN(tmp)) == 0) return Qtrue;
if ((slen = RSTRING_LEN(str)) < tlen) continue;
p = RSTRING_PTR(str);
e = p + slen;
s = e - tlen;
if (!at_char_boundary(p, s, e, enc))
continue;
if (memcmp(s, RSTRING_PTR(tmp), tlen) == 0)
return Qtrue;
}
return Qfalse;
}
Returns whether self ends with any of the given strings.
Returns true if any given string matches the end, false otherwise:
Example
'hello'.end_with?('ello') #=> true 'hello'.end_with?('heaven', 'ello') #=> true 'hello'.end_with?('heaven', 'paradise') #=> false 'тест'.end_with?('т') # => true 'こんにちは'.end_with?('は') # => true
Related: String#start_with?.