How do I override a class method on an object with another module? For example:
"overridden by TestExtension"
puts TestObject.my_method #=> "override me"
puts TestObject.another_method #=> "another method"
Why can't I get TestObject to use my_method from TestExtension?
What you're seeing is that in the method lookup path, a given class
comes before any modules it includes. In your case, the class in
question is the singleton class of TestObject: that class defines
#my_method, and also includes a module that defines #my_method. The
one defined in the class "wins".
Here's another, parallel example:
c = C.new
class << c # defined the method in the class
c.extend(M) # also include the module in the class
c.my_method # "C#my_method" (the class wins)
The module would win if the method were defined in the *class* of the
object, rather than the singleton class -- because then the lookup
order would be:
singleton class (no)
module included by singleton class (yes -- execute method)
[class (never reached)]
On Wed, 30 Aug 2006, John W. Long wrote:
David A. Black | email@example.com
Author of "Ruby for Rails"  | Ruby/Rails training & consultancy 
DABlog (DAB's Weblog)  | Co-director, Ruby Central, Inc. 
 http://www.manning.com/black |  http://www.rubypowerandlight.com
 http://dablog.rubypal.com |  http://www.rubycentral.org