T
Thomas Sawyer
David said:etc. Jay's example with extending an object with two modules, so that
the most recent one "wins" (whereas all modules "lose" if the method
in question is defined in the singleton class) is very convincing,
though again I wouldn't necessarily treat it as a reason never to
define a method in the singleton class. I'd say it's more that you
want to know what the ramifications of each technique are, and then
choose the one you want. In that sense it's analogous (though not
identical) to the non-singleton case, where a method defined in a
class "wins", for the instances of the class, over definitions in
included modules. That can be good, or not; it depends on what you're
trying to do.
It all comes down to mastery of the order of method lookup, which then
allows you to do pretty much any permutation.
Can you think of a good example of where it is NOT good? All I can think
of is when we need to metaprogram around someone else not using modules.
I'm sure there are occasions to act directly on the singleton, but I've
come to believe that should be the exception rather than the rule. And I
think it would behoove Ruby to promote that as the common idiom. We've
grown accustom to seeing "class << self". But I think it should be
raising flags instead --"we are doing something very metay here".
T.