From: "David A. Black" Date: 2004-08-14T23:13:00+09:00 Subject: Re: Opinion on ClassInherit Include Hi -- On Sat, 14 Aug 2004, T. Onoma wrote: > On Saturday 14 August 2004 09:41 am, ts wrote: > > svg% cat b.rb > > #!/usr/bin/ruby > > module MyMod > > attr_accessor :x > > def hix > > puts "Hi, #{@x}." > > end > > end > > > > class A > > extend MyMod > > end > > > > A.x = 'you' > > A.hix #=> Hi, you. > > svg% > > You missed the '# ...' in the example. So that dosen't work. You end up having > to have two seperate modules, one of which is included the other of which is > extended. I want a single encapsulation. To clarify: > > module MyMod >     module ClassInherit >       attr_accessor :x >       def hix >         puts "Hi, #{@x}." >       end >     end >     def hey > puts "Me, too." > end >   end > >   class A >     include MyMod >   end > >   A.x = 'you' >   A.hix  #=> Hi, you. > A.new.hey #=> Me, too. As Robert pointed out, you've already got two modules. I think that's OK, and it's probably better just to do the most direct thing with them (i.e., use include and extend to modify the behavior of the objects you want to change, rather than add a special-case layer for extending class objects). If you want to have them nested, you could perhaps do: module MyMod module Inner attr_accessor :x def hix puts "Hi, #{@x}." end end def blah puts "hi" end end class A include MyMod extend Inner end A.x = 'you' A.hix # => Hi, you. A.new.blah # => hi David -- David A. Black dblack@wobblini.net