From: Daniel Berger Date: 2006-04-11T07:25:07+09:00 Subject: Re: Adding both class and instance/object methods from a Module? ara.t.howard@noaa.gov wrote: > On Tue, 11 Apr 2006, Sam Roberts wrote: > >> On Tue, Apr 11, 2006 at 02:46:24AM +0900, zdennis wrote: >>>> Is there an easy way to extend classes with both class and object >>>> methods at the same time? >> >>> irb(main):010:1* def self.included( clazz ) >>> irb(main):011:2> clazz.extend( ClassLevelAddons ) >>> irb(main):012:2> end >>> irb(main):013:1> end >>> >>> The above lets M take care of the work, so your class doesn't have to >>> think about it. This is the method that Robert Klemme was refering to, >>> Module::included >> >> I was hoping that I was missing some clever way to do it with just one >> module. Guess not. But the self.included hook is a good idea. The two >> modules become an implementation detail, and thats good enough. > > you can do it with one: > > > harp:~ > cat a.rb > module M > def a > self > end > def self.included other > other.extend self > super > end > end > > class C > include M > end > > p C.a > p C.new.a > > > harp:~ > ruby a.rb > C > # > > > regards. > > -a I'm coming into this thread late, but I think I'm missing something here. What's wrong with this? module Foo attr_accessor :meth_a end class Bar include Foo extend Foo end p Bar.meth_a # nil Bar.meth_a = 'test' p Bar.meth_a # 'test' bar = Bar.new p bar.meth_a # nil bar.meth_a = 'test' p bar.meth_a # 'test' Regards, Dan