From: Vincent Fourmond Date: 2006-12-22T01:59:37+09:00 Subject: Re: extend weirdness? Ian White wrote: > Hi Group, > > This code: > module Foo > def self.included(base) > base.class_eval <<-end_eval > def self.meth > "Foo" > end > end_eval > end > end > > module Bar > def self.included(base) > base.class_eval do > extend ClassMethods > end > end > > module ClassMethods > def meth > "Bar" > end > end > end > > class A > include Foo > include Bar > end > > puts A.meth > > produces: > Foo > > whereas I would expect it to produce "Bar" > > Can anyone explain what's going on here? i.e. Why the latter extend > with Bar doesn't over-write the class method introduced by Foo The answer is simple: extend doesn't overwrite methods, it just includes the module for the singleton object. As there is already a singleton method 'meth' defined in the singleton object A, this method is used systematically in method calls. Does that explain your result ? For information, extend is rb_extend_object(obj, module) VALUE obj, module; { rb_include_module(rb_singleton_class(obj), module); } It is basically like class << self include Something end Cheers, Vince -- Vincent Fourmond, PhD student http://vincent.fourmond.neuf.fr/