From: "Anders Engström" Date: 2004-08-15T00:47:51+09:00 Subject: Re: Opinion on ClassInherit Include --h31gzZEtNLTqOjlF Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Sat, Aug 14, 2004 at 10:54:28PM +0900, 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 =3D 'you' > > A.hix #=3D> Hi, you. > > svg% >=20 > You missed the '# ...' in the example. So that dosen't work. You end up h= aving=20 > to have two seperate modules, one of which is included the other of which= is=20 > extended. I want a single encapsulation. To clarify: >=20 > module MyMod > =A0 =A0 module ClassInherit > =A0 =A0 =A0 attr_accessor :x > =A0 =A0 =A0 def hix > =A0 =A0 =A0 =A0 puts "Hi, #{@x}." > =A0 =A0 =A0 end > =A0 =A0 end > =A0 =A0 def hey > puts "Me, too." > end > =A0 end >=20 > =A0 class A > =A0 =A0 include MyMod > =A0 end >=20 > =A0 A.x =3D 'you' > =A0 A.hix =A0#=3D> Hi, you. > A.new.hey #=3D> Me, too. >=20 The way I handle these situations is: module MyMod =20 class << self def included(klass) # Also extend the class with # the features in ClassMixin klass.__send__(:extend, ClassMixin) end end =20 module ClassMixin attr_accessor :x def hix puts "Hi, #{@x}." end end =20 def hey puts "Me, too" end end class A include MyMod end HTH. //Anders --=20 =2E . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . =2E Anders Engstr=F6m aengstrom@gnejs.net =2E http://www.gnejs.net PGP-Key: ED010E7F =2E [Your mind is like an umbrella. It doesn't work unless you open it.] = =20 --h31gzZEtNLTqOjlF Content-Type: application/pgp-signature; name="signature.asc" Content-Description: Digital signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.4 (GNU/Linux) iD8DBQFBHjOkuNLLbe0BDn8RAtNsAJ9c2m9V0UVFOSkMmqrgY3h+SpgwcgCfUPTP 9uV/hErI9ZLVdK+RQPtoyxY= =Ls06 -----END PGP SIGNATURE----- --h31gzZEtNLTqOjlF--