Hey guys,
I thought I understood super just fine until I ran into a little issue
recently. It’s not really documented in ruby-doc as it’s a keyword and
such… and the explanations I’ve received vary widely.
Lots of people say: "Oh, super just calls the method of the same name in
the parent class (an instance method for an instance of a class, or a
singleton method if an instance of Class or Module), and it just goes up
the inheritance chain until up to BasicObject or until it finds the
method name and then if nothing is found it raises an error.
Also, self is set to the scope of self in the method originally calling
super() and an error is raised there as well if nothing is found.
Fine…
But the issue I ran into was calling super in a singleton method of a
class object ie: Someclass.somemethod(), and hoping that my included
module that also defined a singleton method would catch the call to
super().
It turns out that module objects don’t catch the super call, and they
also don’t catch method_missing and other hooks if they’re included but
still define singleton methods.
Why is this?
I’ve been thinking about it, and is it because the anonymous class that
gets put into the inheritance chain doesn’t get the singleton method at
all… like since only the instance methods, class variables and
constants are in that anon. class?
And further, is it because the anonymous class is skipped entirely in
the call to a class’s .superclass() method? And super only goes directly
to the superclasses to check the method?
I have lots of questions, and an answer as to what’s possible and what’s
not, but I’d really like an explanation as to why this is happening this
way.
Simple illustrative code snippet:
Thanks for taking the time,
-Luke