# Adding in class attribute with a Module

**URL:** <https://rubytalk.org/t/adding-in-class-attribute-with-a-module/26548>\
**Category:** ruby-talk\
**Created:** [10 April 2006 14:58 UTC](https://rubytalk.org/t/adding-in-class-attribute-with-a-module/26548 "2006-04-10T14:58:43Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![7rans](https://avatars.discourse-cdn.com/v4/letter/7/a8b319/32.png) [@7rans](https://rubytalk.org/u/7rans)\
**Post date:** [10 April 2006 14:58 UTC](https://rubytalk.org/t/adding-in-class-attribute-with-a-module/26548/1 "2006-04-10T14:58:43Z")

</div>

> Is it possible to add class attributes (cattr\_accessor) using a module?  
> If so could you give an example?

There is no "Ruby Way" to do this b/c Ruby's module methods aren't  
inherited by classes that include them. Though I've asked for a way  
man times now!

It might seem easy to glaze by this, thinking nothing of  
cattr\_accessor, a seemingly esoteric method since it's not included in  
core Ruby, but you will find it in both Rail's Active Support and Ruby  
Facets. And in fact, you've brought up a nice example of how this play  
out b/c cattr\_accessor and it's ilk really don't do anything very  
esoteric under the hood. I'm going to layout what cattr\_accessor  
essentially does\* so all can see this for what it is and what happens  
when you try to REUSE structures of this nature.

For a class, it is simply:

&nbsp;&nbsp;class C  
&nbsp;&nbsp;&nbsp;&nbsp;def self.m ; @m ; end  
&nbsp;&nbsp;&nbsp;&nbsp;def self.m=(x) ; @m=x ; end  
&nbsp;&nbsp;&nbsp;&nbsp;def m ; self.class.m ; end  
&nbsp;&nbsp;&nbsp;&nbsp;def m=(x) ; self.class.m=x ; end  
&nbsp;&nbsp;end

Hence

&nbsp;&nbsp;C.m = 10  
&nbsp;&nbsp;C.m #=\> 10  
&nbsp;&nbsp;c = C.new  
&nbsp;&nbsp;c.m #=\> 10  
&nbsp;&nbsp;c.m = 20  
&nbsp;&nbsp;c.m #=\> 20  
&nbsp;&nbsp;C.m #=\> 20

While you can subclass C with this functionality readily, eg 'class N  
\< C' works as you'd expect, try MODULARIZING this functionality. The  
cooresponding

&nbsp;&nbsp;module M  
&nbsp;&nbsp;&nbsp;&nbsp;def self.m ; @m ; end  
&nbsp;&nbsp;&nbsp;&nbsp;def self.m=(x) ; @m=x ; end  
&nbsp;&nbsp;&nbsp;&nbsp;def m ; self.class.m ; end  
&nbsp;&nbsp;&nbsp;&nbsp;def m=(x) ; self.class.m=x ; end  
&nbsp;&nbsp;end

does not work at all. You can't include it in a class, you can't  
extend a class with it, nor both --it completely bombs. Feel free to  
maniputlate the code as well --shame as that is, while you can get  
closer, nothing completely works --there's alwasy something wrong.  
Only ugly meta-programming hacks will give the sought behavior --and  
even then their are corner-case complications.

T.

\* I'm using an instance var in this case rather than a class var for  
simplicity sake, but it makes no difference to the problem itself.

---

<div class="post-metadata">

**Author:** ![Gregory\_Seidman](https://avatars.discourse-cdn.com/v4/letter/g/eb9ed0/32.png) [@Gregory\_Seidman](https://rubytalk.org/u/Gregory_Seidman)\
**Post date:** [10 April 2006 15:19 UTC](https://rubytalk.org/t/adding-in-class-attribute-with-a-module/26548/2 "2006-04-10T15:19:12Z")

</div>

} \> Is it possible to add class attributes (cattr\_accessor) using a module?  
} \> If so could you give an example?  
}  
} There is no "Ruby Way" to do this b/c Ruby's module methods aren't  
} inherited by classes that include them. Though I've asked for a way  
} man times now!  
[...]  
} Only ugly meta-programming hacks will give the sought behavior --and  
} even then their are corner-case complications.

Does this count as ugly meta-programming?

module M  
&nbsp;&nbsp;def self.included(klass)  
&nbsp;&nbsp;&nbsp;&nbsp;class \<\< klass  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;attr\_accessor :m  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
end

} T.  
--Greg

> **···**
>
> On Mon, Apr 10, 2006 at 11:58:43PM +0900, TRANS wrote:

---

<div class="post-metadata">

**Author:** ![Zach\_Dennis1](https://avatars.discourse-cdn.com/v4/letter/z/ea5d25/32.png) [@Zach\_Dennis1](https://rubytalk.org/u/Zach_Dennis1)\
**Post date:** [10 April 2006 16:02 UTC](https://rubytalk.org/t/adding-in-class-attribute-with-a-module/26548/3 "2006-04-10T16:02:32Z")

</div>

TRANS wrote:

> > Is it possible to add class attributes (cattr\_accessor) using a module?  
> > If so could you give an example?
> 
> There is no "Ruby Way" to do this b/c Ruby's module methods aren't  
> inherited by classes that include them. Though I've asked for a way  
> man times now!
> 
> It might seem easy to glaze by this, thinking nothing of  
> cattr\_accessor, a seemingly esoteric method since it's not included in  
> core Ruby, but you will find it in both Rail's Active Support and Ruby  
> Facets. And in fact, you've brought up a nice example of how this play  
> out b/c cattr\_accessor and it's ilk really don't do anything very  
> esoteric under the hood. I'm going to layout what cattr\_accessor  
> essentially does\* so all can see this for what it is and what happens  
> when you try to REUSE structures of this nature.
> 
> For a class, it is simply:
> 
> &nbsp;&nbsp;class C  
> &nbsp;&nbsp;&nbsp;&nbsp;def self.m ; @m ; end  
> &nbsp;&nbsp;&nbsp;&nbsp;def self.m=(x) ; @m=x ; end  
> &nbsp;&nbsp;&nbsp;&nbsp;def m ; self.class.m ; end  
> &nbsp;&nbsp;&nbsp;&nbsp;def m=(x) ; self.class.m=x ; end  
> &nbsp;&nbsp;end
> 
> Hence
> 
> &nbsp;&nbsp;C.m = 10  
> &nbsp;&nbsp;C.m #=\> 10  
> &nbsp;&nbsp;c = C.new  
> &nbsp;&nbsp;c.m #=\> 10  
> &nbsp;&nbsp;c.m = 20  
> &nbsp;&nbsp;c.m #=\> 20  
> &nbsp;&nbsp;C.m #=\> 20

On a total side note, why do you have cattr\_\* methods affecting instance methods as well ?

&nbsp;&nbsp;class C  
&nbsp;&nbsp;&nbsp;&nbsp;cattr\_accessor :m  
&nbsp;&nbsp;end

&nbsp;&nbsp;C.m = 10  
&nbsp;&nbsp;c = C.new  
&nbsp;&nbsp;puts c.m  
&nbsp;&nbsp;# =\> 10

I would expect it to work for "C.m" and "C.m=" but not for "c.m" I would expect:

&nbsp;&nbsp;NoMethodError: undefined method `m' for #\<C:0xb7d3103c\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from /home/zdennis/.irbrc:4:in `method\_missing'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):5

Perhaps I have wrong expectation of cattr\_\* . I am just asking at this point though to better understand thought behind it.

Zach

> **···**
>
> from :0

---

<div class="post-metadata">

**Author:** ![7rans](https://avatars.discourse-cdn.com/v4/letter/7/a8b319/32.png) [@7rans](https://rubytalk.org/u/7rans)\
**Post date:** [10 April 2006 21:19 UTC](https://rubytalk.org/t/adding-in-class-attribute-with-a-module/26548/4 "2006-04-10T21:19:23Z")

</div>

> Does this count as ugly meta-programming?
> 
> module M  
> &nbsp;&nbsp;def self.included(klass)  
> &nbsp;&nbsp;&nbsp;&nbsp;class \<\< klass  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;attr\_accessor :m  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end  
> end

Yep. That's Ugly Hack #1. What wrong with Ugly Hack #1, you ask?

&nbsp;&nbsp;module N  
&nbsp;&nbsp;&nbsp;&nbsp;include M  
&nbsp;&nbsp;end

&nbsp;&nbsp;class C  
&nbsp;&nbsp;&nbsp;&nbsp;include N  
&nbsp;&nbsp;end

&nbsp;&nbsp;C.m  
&nbsp;&nbsp;NoMethodError: undefined method `m' for C:Class

T.

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [10 April 2006 17:19 UTC](https://rubytalk.org/t/adding-in-class-attribute-with-a-module/26548/5 "2006-04-10T17:19:30Z")

</div>

> } \> Is it possible to add class attributes (cattr\_accessor) using a  
> module?  
> } \> If so could you give an example?  
> }  
> } There is no "Ruby Way" to do this b/c Ruby's module methods aren't  
> } inherited by classes that include them. Though I've asked for a way  
> } man times now!  
> [...]  
> } Only ugly meta-programming hacks will give the sought behavior --and  
> } even then their are corner-case complications.
> 
> Does this count as ugly meta-programming?
> 
> module M  
> &nbsp;&nbsp;def self.included(klass)  
> &nbsp;&nbsp;&nbsp;&nbsp;class \<\< klass  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;attr\_accessor :m  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end  
> end

It is much prettier than the following ugly meta-programming, but I have no  
clue how it works !!!  
Could you explain please?  
Thx  
Robert

} T.

> --Greg

class Module

> **···**
>
> On 4/10/06, Gregory Seidman \<gsslist+ruby@anthropohedron.net\> wrote:
> 
> > On Mon, Apr 10, 2006 at 11:58:43PM +0900, TRANS wrote:  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def cattr( \*names )  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;names.map{ |name| name.to\_s }.each do  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\>name\>  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;eval \<\<-EOEval  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def self.#{name}; @#{name}; end  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def self.#{name}=(x); @#{name}=x; end  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;EOEval  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;define\_method( name ){ self.class.send( name ) }  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;define\_method( name + "="){ |v| self.class.send(  
> > name + "=", v ) }  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > end
> 
> --  
> Deux choses sont infinies : l'univers et la bêtise humaine ; en ce qui  
> concerne l'univers, je n'en ai pas acquis la certitude absolue.
> 
> - Albert Einstein

---

<div class="post-metadata">

**Author:** ![Gregory\_Seidman](https://avatars.discourse-cdn.com/v4/letter/g/eb9ed0/32.png) [@Gregory\_Seidman](https://rubytalk.org/u/Gregory_Seidman)\
**Post date:** [11 April 2006 14:29 UTC](https://rubytalk.org/t/adding-in-class-attribute-with-a-module/26548/6 "2006-04-11T14:29:32Z")

</div>

} \> Does this count as ugly meta-programming?  
} \>  
} \> module M  
} \> def self.included(klass)  
} \> class \<\< klass  
} \> attr\_accessor :m  
} \> end  
} \> end  
} \> end  
}  
} Yep. That's Ugly Hack #1. What wrong with Ugly Hack #1, you ask?  
}  
} module N  
} include M  
} end  
}  
} class C  
} include N  
} end  
}  
} C.m  
} NoMethodError: undefined method `m' for C:Class

Yep, that's an issue. Well, you can do this:

module M  
&nbsp;&nbsp;OnIncludeProc = lambda do |klass|  
&nbsp;&nbsp;&nbsp;&nbsp;if klass.class == Module  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def klass.included(k)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;OnIncludeProc.call(k)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;class \<\< klass  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;attr\_accessor :m  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end

&nbsp;&nbsp;OnIncludeProc.call(self)

end

Yes, it's uglier. Yes, it conflicts with other #included tricks with modules  
that include the module. It does work, though.

} T.  
--Greg

> **···**
>
> On Tue, Apr 11, 2006 at 06:19:23AM +0900, TRANS wrote:

---

<div class="post-metadata">

**Author:** ![Gregory\_Seidman](https://avatars.discourse-cdn.com/v4/letter/g/eb9ed0/32.png) [@Gregory\_Seidman](https://rubytalk.org/u/Gregory_Seidman)\
**Post date:** [10 April 2006 17:40 UTC](https://rubytalk.org/t/adding-in-class-attribute-with-a-module/26548/7 "2006-04-10T17:40:35Z")

</div>

} \>  
} \> On Mon, Apr 10, 2006 at 11:58:43PM +0900, TRANS wrote:  
} \> } \> Is it possible to add class attributes (cattr\_accessor) using a  
} \> module?  
} \> } \> If so could you give an example?  
} \> }  
} \> } There is no "Ruby Way" to do this b/c Ruby's module methods aren't  
} \> } inherited by classes that include them. Though I've asked for a way  
} \> } man times now!  
} \> [...]  
} \> } Only ugly meta-programming hacks will give the sought behavior --and  
} \> } even then their are corner-case complications.  
} \>  
} \> Does this count as ugly meta-programming?  
} \>  
} \> module M  
} \> def self.included(klass)  
} \> class \<\< klass  
} \> attr\_accessor :m  
} \> end  
} \> end  
} \> end  
}  
} It is much prettier than the following ugly meta-programming, but I have no  
} clue how it works !!!  
} Could you explain please?

The first thing to understand is that when you define a class, e.g. class  
Foo; ... end, you are just setting a constant (Object::Foo, in this case)  
to a new instance of a Class. Just as you can add new methods to an  
instance of any other kind of object with class \<\< obj; ... end, you can  
add new methods to Foo. When a module is included, its self.included method  
is called with the Class instance it is being included into as an argument.  
I am defining self.included to add an attribute accessor to that Class  
instance.

} Thx  
} Robert  
[...]  
--Greg

> **···**
>
> On Tue, Apr 11, 2006 at 02:19:30AM +0900, Robert Dober wrote:  
> } On 4/10/06, Gregory Seidman \<[gsslist+ruby@anthropohedron.net](mailto:gsslist+ruby@anthropohedron.net)\> wrote:

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [10 April 2006 19:18 UTC](https://rubytalk.org/t/adding-in-class-attribute-with-a-module/26548/8 "2006-04-10T19:18:15Z")

</div>

> } \>  
> } \> On Mon, Apr 10, 2006 at 11:58:43PM +0900, TRANS wrote:  
> } \> } \> Is it possible to add class attributes (cattr\_accessor) using a  
> } \> module?  
> } \> } \> If so could you give an example?  
> } \> }  
> } \> } There is no "Ruby Way" to do this b/c Ruby's module methods aren't  
> } \> } inherited by classes that include them. Though I've asked for a way  
> } \> } man times now!  
> } \> [...]  
> } \> } Only ugly meta-programming hacks will give the sought behavior --and  
> } \> } even then their are corner-case complications.  
> } \>  
> } \> Does this count as ugly meta-programming?  
> } \>  
> } \> module M  
> } \> def self.included(klass)  
> } \> class \<\< klass  
> } \> attr\_accessor :m  
> } \> end  
> } \> end  
> } \> end  
> }  
> } It is much prettier than the following ugly meta-programming, but I have  
> no  
> } clue how it works !!!  
> } Could you explain please?
> 
> The first thing to understand is that when you define a class, e.g. class  
> Foo; ... end, you are just setting a constant (Object::Foo, in this case)  
> to a new instance of a Class. Just as you can add new methods to an  
> instance of any other kind of object with class \<\< obj; ... end, you can  
> add new methods to Foo. When a module is included, its self.includedmethod  
> is called with the Class instance it is being included into as an  
> argument.

I see thank you about the explanation of that hook.

I am defining self.included to add an attribute accessor to that Class

> instance.

No of course not, because it is equivalent to

class \<\<self  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;attr\_accessor  
end

which does not work.

} Thx

> } Robert  
> [...]  
> --Greg

Robert

> **···**
>
> On 4/10/06, Gregory Seidman \<gsslist+ruby@anthropohedron.net\> wrote:
> 
> > On Tue, Apr 11, 2006 at 02:19:30AM +0900, Robert Dober wrote:  
> > } On 4/10/06, Gregory Seidman \<gsslist+ruby@anthropohedron.net\> wrote:
> 
> --  
> Deux choses sont infinies : l'univers et la bêtise humaine ; en ce qui  
> concerne l'univers, je n'en ai pas acquis la certitude absolue.
> 
> - Albert Einstein
