Browsing through the MRI codebase, I find that some aliased methods are defined as aliases, but other times not.
For example, TrueClass#inspect is an alias to its #to_s (source):
rb_define_alias(rb_cTrueClass, "inspect", "to_s");
... but Object#is_a? is not an alias for kind_of?, instead, they define two separate methods with the same implementing function (source):
rb_define_method(rb_mKernel, "kind_of?", rb_obj_is_kind_of, 1);
rb_define_method(rb_mKernel, "is_a?", rb_obj_is_kind_of, 1);
This distinction is observable from "userspace" Ruby, too:
# Aliased methods have a different `original_name`:
true.method(:inspect).original_name # => :to_s
# "Duplicated" methods don't:
true.method(:is_a?).original_name # => :is_a?
true.method(:kind_of?).original_name # => :kind_of?
Is there a pattern to when aliases used, vs. when they're not? Does it make a difference?
This is by no means an authoritative answer to the question, nor does it truly answer the questions posed; however, it does provide some distinguishing characteristics between the options provided and was far too verbose to meet the format of a reasonable comment.
Questions
I can't say. Possibly only Matz or a part of the core team could answer this for you.
It could as shown here.
There is a notable difference between aliasing and defining multiple methods with the same implementation, when it comes to redefinition of the function1 (or method) referenced.
Aliasing
rb_define_aliasactually clones the function definition when creating the alias usingrb_method_entry_clone.Since the function is actually cloned (and not just referenced) any subsequent change to
rb_mod_to_s(the functionto_srefers to) or redefinition of the actualto_smethod within Ruby code will not impact the definition ofinspect, which will still represent the original function.This is how
aliasandalias_method2 in higher level ruby work as well, using essentially the same function calls asrb_define_aliasSeparate Method Definitions with the same implementation
rb_define_methodsimply creates a method reference to the underlying function, so should the implementation ofrb_obj_is_kind_ofchange, such change would impact bothkind_of?andis_a?since they are directly referencing that function.Higher Level Example
We can see similar behavior within Ruby:
Also note even though the definition of
(1) The term function was used in this post because the question is specifically referring to the underlying C implementation. Ruby as a language does not have functions.to_shas changed the "original name" for bothinspectandother_inspectwill still returnto_s.(2) There are distinct differences between
aliasandalias_methodbeyond the scope of this question