On Sat, Oct 16, 2010 at 4:19 PM, David A. Black [email protected]
wrote:
I could be wrong, and haven’t dug into the code, but I’ve always had the
impression that (by design) there’s no difference between different ways
to refer to a method. It’s not that the alias is a kind of nickname for
the “real” name; it is (one of) the real name(s).
You inspired me to take a look and you are right on. alias just
creates a new entry for the same method into the target’s method
table. There is nothing in the entry that distinguishes it as an
alias.
In 1.9.2, anyway, #instance_methods seems to list methods in the order
they were created/aliased, though I don’t know whether this is
guaranteed:
The methods are always copied in the order they were defined (using
rb_ary_push). I guess that’s guaranteed enough for a hack. Based on
the equality Daniel pointed out and the ordering you did, I took a jab
at it:
On Sat, Oct 16, 2010 at 3:19 PM, David A. Black [email protected]
wrote:
In 1.9.2, anyway, #instance_methods seems to list methods in the
order
Maybe it is best to say that somehow it really does not matter, I
would be curious to see where it matters. The following, completely
useless code, somehow demonstrates this:
Class::new do
def a; end
alias_method :x, :a
remove_method :a
alias_method :a, :x
p instance_methods( false )
end
and the question I would like to ask is, what is :a in this case? An
alias to itself after having been removed I guess ;). But I prefer
David’s notation above which is the perfect explanation of what is
really happening here.(1)
Hopefully that kind of manipulation can be safely ignored when talking
about “definition order”.
(1) Maybe with the exception that remove_method “really” means, remove
the entry with this name pointing to the method, but by all means let
us keep the name :).
and the question I would like to ask is, what is :a in this case? An
alias to itself after having been removed I guess ;).
If I understand how this works correctly, then remove_method is only
removing the :a entry for the method from the object’s table, but
since there is another “reference” to the method definition (the b
alias, which is just another entry to same method), the body of the
method is not removed. Adding :a again is simply adding a new entry
for the same method. If both :a and :b are removed, then one tries to
add an alias a NameError would be raised.