"Adelle Hartley" <adelle / bullet.net.au> schrieb im Newsbeitrag
news:courier.4242A14D.00002361 / mars.sisgroup.com.au...
> Hi all,
>
> I have some methods that I want to add to several classes.
> eg.
>
>   class A << MyBaseClass
>     def self.DoMyThing(klass=self)
>       ...
>     end
>   end
>
>   class B << MyBaseClass
>     def self.DoMyThing(klass=self)
>       ...
>     end
>   end
>
>   class C << B
>     def self.DoMyThing(klass=self)
>       ...
>     end
>   end
>
> If I were writing this in C++, I'd use templates.  What's the Ruby Way?

module Extension
  # this one is really only neede if you
  # want to inherit this behavior automatically
  # down the class hierarchy
  def inherited(cl)
    cl.extend Extension
  end

  def do_my_thing(klass=self)
    # whatever
    p klass
  end
end

>> class A
>>   extend Extension
>> end
=> A
>> A.do_my_thing
A
=> nil
>> class B < A
>> end
=> nil
>> B.do_my_thing
B
=> nil

Kind regards

    robert