Ruby on Rails has the method String#starts_with?, which is implemented in Ruby with
# File lib/active_support/core_ext/string/starts_ends_with.rb, line 22
def starts_with?(prefix)
prefix = prefix.to_s
self[0, prefix.length] == prefix
end
whereas Ruby, since version 1.8.7, has the method String#start_with?, which is implemented in C
static VALUE
rb_str_start_with(int argc, VALUE *argv, VALUE str)
{
int i;
for (i=0; i<argc; i++) {
VALUE tmp = rb_check_string_type(argv[i]);
if (NIL_P(tmp)) continue;
rb_enc_check(str, tmp);
if (RSTRING_LEN(str) < RSTRING_LEN(tmp)) continue;
if (memcmp(RSTRING_PTR(str), RSTRING_PTR(tmp), RSTRING_LEN(tmp)) == 0)
return Qtrue;
}
return Qfalse;
}
Why don’t they just have an alias linking from starts_with? to start_with??
Do they wish to maintain compatibility with Ruby 1.8.6, or are they worried that start_with? may have different behavior to starts_with?, or have they not seen the need to update the code?
From your code, it looks like you have a really old version of Rails at hand (<= 2.3.5). As this version supported older Ruby versions, which didn’t yet had
start_with?they implemented it by hand.However if you look above in the file, you see that they used the ruby built-in method if it is available. In newer Rails versions where the Ruby 1.8.6 support was dropped, these magic bits were removed and ActiveSupport now provides unconditional aliases.