--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 = '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 whichs 
> 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.
> 

The way I handle these situations is:

module MyMod
    
    class << self
        def included(klass)
            # Also extend the class with
            # the features in ClassMixin
            klass.__send__(:extend, ClassMixin)
        end
    end
    
    module ClassMixin
        attr_accessor :x
        def hix
            puts "Hi, #{@x}."
        end
    end
    
    def hey
        puts "Me, too"
    end
end

class A
    include MyMod
end

HTH. //Anders

-- 
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. Anders Engströí         aengstrom / gnejs.net
. http://www.gnejs.net    PGP-Key: ED010E7F
. [Your mind is like an umbrella. It doesn't work unless you open it.]  


--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+RQPtoyxY06
-----END PGP SIGNATURE-----

--h31gzZEtNLTqOjlF--