# Metaprogramming to generate list of class-specific attributes

**URL:** <https://rubytalk.org/t/metaprogramming-to-generate-list-of-class-specific-attributes/40623>\
**Category:** ruby-talk\
**Created:** [14 September 2007 21:00 UTC](https://rubytalk.org/t/metaprogramming-to-generate-list-of-class-specific-attributes/40623 "2007-09-14T21:00:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Phlip1](https://avatars.discourse-cdn.com/v4/letter/p/df788c/32.png) [@Phlip1](https://rubytalk.org/u/Phlip1)\
**Post date:** [14 September 2007 21:00 UTC](https://rubytalk.org/t/metaprogramming-to-generate-list-of-class-specific-attributes/40623/1 "2007-09-14T21:00:16Z")

</div>

Rubyists:

Here's a test case illustrates the problem. Then we'll have a candidate fix  
and some questions.

&nbsp;&nbsp;class Kozmik  
&nbsp;&nbsp;&nbsp;def self.acts\_as\_frog(species)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@@frog\_list \<\< species  
&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;@@frog\_list = []  
&nbsp;&nbsp;&nbsp;cattr\_accessor :frog\_list  
end

class Toad \< Kozmik  
&nbsp;&nbsp;&nbsp;acts\_as\_frog :bufo  
&nbsp;&nbsp;&nbsp;acts\_as\_frog :kermit  
end

class BullFrog \< Kozmik  
&nbsp;&nbsp;&nbsp;acts\_as\_frog :tree  
&nbsp;&nbsp;&nbsp;acts\_as\_frog :riparian  
end

def test\_fun\_with\_metaclasses  
&nbsp;&nbsp;&nbsp;assert\_equal [:tree, :riparian], BullFrog.frog\_list  
&nbsp;&nbsp;&nbsp;assert\_equal [:bufo, :kermit], Toad.frog\_list  
end

Suppose we need an 'acts\_as\_' system to generate some methods for our target  
classes. And suppose one method is a list of all the arguments passed to  
each acts\_as\_() in each specific class.

The above test fails because @@ occupies the entire inheritance chain. So  
both frog\_list() class-methods return [:tree, :riparian, :bufo, :kermit].  
The fix seems to be to reach into the meta-class for each derived class:

class Kozmik  
&nbsp;&nbsp;&nbsp;def self.acts\_as\_frog(species)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@frog\_list ||= []  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@frog\_list \<\< species  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.class.send :attr\_accessor, :frog\_list  
&nbsp;&nbsp;&nbsp;end  
end

Is that as clean and lean as it could be? Is there some way to do this with  
even fewer lines?

Next question: Couldn't class Kozmik be a module instead?

> **···**
>
> --  
> Phlip  
> [http://www.oreilly.com/catalog/9780596510657/](http://www.oreilly.com/catalog/9780596510657/)  
> ^ assert\_xpath

---

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf1](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@Joel\_VanderWerf1](https://rubytalk.org/u/Joel_VanderWerf1)\
**Post date:** [14 September 2007 21:21 UTC](https://rubytalk.org/t/metaprogramming-to-generate-list-of-class-specific-attributes/40623/2 "2007-09-14T21:21:43Z")

</div>

Phlip wrote:  
...

> class Kozmik  
> &nbsp;&nbsp;&nbsp;def self.acts\_as\_frog(species)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@frog\_list ||=   
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@frog\_list \<\< species  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.class.send :attr\_accessor, :frog\_list  
> &nbsp;&nbsp;&nbsp;end  
> end
> 
> Is that as clean and lean as it could be? Is there some way to do this with  
> even fewer lines?
> 
> Next question: Couldn't class Kozmik be a module instead?

Sure...

&nbsp;&nbsp;module Kozmik  
&nbsp;&nbsp;&nbsp;&nbsp;def acts\_as\_frog(species)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@frog\_list ||=   
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@frog\_list \<\< species  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.class.send :attr\_accessor, :frog\_list  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end

&nbsp;&nbsp;class Toad; extend Kozmik  
&nbsp;&nbsp;...  
&nbsp;&nbsp;end

Or is that cheating somehow?

> **···**
>
> --  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

---

<div class="post-metadata">

**Author:** ![Ari\_Brown](https://avatars.discourse-cdn.com/v4/letter/a/48db29/32.png) [@Ari\_Brown](https://rubytalk.org/u/Ari_Brown)\
**Post date:** [14 September 2007 21:45 UTC](https://rubytalk.org/t/metaprogramming-to-generate-list-of-class-specific-attributes/40623/3 "2007-09-14T21:45:15Z")

</div>

> Rubyists:

Here is one answering

> class Kozmik  
> &nbsp;&nbsp;&nbsp;def self.acts\_as\_frog(species)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@frog\_list ||=   
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@frog\_list \<\< species  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.class.send :attr\_accessor, :frog\_list  
> &nbsp;&nbsp;&nbsp;end  
> end
> 
> Is that as clean and lean as it could be? Is there some way to do this with  
> even fewer lines?

I am not really sure if this works, but couldn't you do:

class Kozmik  
&nbsp;&nbsp;&nbsp;def self.inherited  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;module\_eval do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@frog\_list =   
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;def self.acts\_as\_frog(species)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@frog\_list \<\< species  
&nbsp;&nbsp;&nbsp;end  
end

Also, does module\_eval only work in a class method? Why can't I do def something; module\_eval do puts 5 end; end ?

Ari  
-------------------------------------------|  
Nietzsche is my copilot

> **···**
>
> On Sep 14, 2007, at 5:00 PM, Phlip wrote:

---

<div class="post-metadata">

**Author:** ![Phlip1](https://avatars.discourse-cdn.com/v4/letter/p/df788c/32.png) [@Phlip1](https://rubytalk.org/u/Phlip1)\
**Post date:** [14 September 2007 21:27 UTC](https://rubytalk.org/t/metaprogramming-to-generate-list-of-class-specific-attributes/40623/4 "2007-09-14T21:27:18Z")

</div>

Joel VanderWerf wrote:

> &nbsp;&nbsp;class Toad; extend Kozmik

extend... different from include, right?

> **···**
>
> --  
> Phlip

---

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf1](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@Joel\_VanderWerf1](https://rubytalk.org/u/Joel_VanderWerf1)\
**Post date:** [14 September 2007 21:38 UTC](https://rubytalk.org/t/metaprogramming-to-generate-list-of-class-specific-attributes/40623/5 "2007-09-14T21:38:09Z")

</div>

Phlip wrote:

> Joel VanderWerf wrote:
> 
> > &nbsp;&nbsp;class Toad; extend Kozmik
> 
> extend... different from include, right?

If you need it to be #include, you can always call #extend from within #append\_features as in ruby-talk:35979.

> **···**
>
> --  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
