From: Robert Klemme Date: 2006-04-10T20:57:04+09:00 Subject: Re: Adding in class attribute with a Module 2006/4/10, zdennis : > -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > I think the OP wanted class level attribute accessor, not instance level accessors. You showed how-to do instance level accessors: > > zdennis@lima:~$ irb > irb(main):001:0> class A ; attr_accessor :foo ; end > => nil > irb(main):002:0> a = A.new > => # > irb(main):003:0> a.foo = 5 > => 5 > irb(main):004:0> a.foo > => 5 > > but using mixins: > > irb(main):007:0> module M ; attr_accessor :foo ; end > => nil > irb(main):008:0> class B ; include M; end > => B > irb(main):009:0> b = B.new > => # > irb(main):010:0> b.foo = 5 > => 5 > irb(main):011:0> b.foo > => 5 > > I think OP wants the equivalent of: > > irb(main):012:0> class C ; class << self ; attr_accessor :foo ; end ; end > => nil > irb(main):013:0> C.foo = 5 > => 5 > irb(main):014:0> C.foo > => 5 > > But with using mixins. The OP should just open up the instance of the class and include the module there. For example: > > irb(main):015:0> class D > irb(main):016:1> class << self > irb(main):017:2> include M > irb(main):018:2> end > irb(main):019:1> end > => # > irb(main):020:0> D.foo = 99 > => 99 > irb(main):021:0> D.foo > => 99 There's an easier way: class D extend M end or class D;end D.extend M There are other options (e.g. use #included) but it depends what the OP really wants. Kind regards robert