Given the various ways of sharing methods in Ruby, and the fact that those methods are themselves objects, what are the underlying mechanics of such sharing? For example, given:
module A
def alpha; end
end
module B
include A
end
is it accurate to say that B's alpha
instance method is a copy of A's? It appears so:
alpha_a = A.instance_method(:alpha)
alpha_b = B.instance_method(:alpha)
alpha_a == alpha_b
=> false
alpha_a.object_id == alpha_b.object_id
=> false
This makes it look (to me) like alpha_b is a copy of alpha_a that belongs to B. But reading about Ruby's handling of method-lookup/dispatch, it sounds the call traverses the ancestors all the way back to A before finding the method to call. Also, there's this:
alpha_b.owner
=> A
So what exactly is going on? Do alpha_a and alpha_b both "reside" in A, despite the latter having been generated when B included A? Do the underlying mechanics depend on the mode of sharing (include/extend/inherit)?