# Metaprogramming surprise?

**URL:** <https://rubytalk.org/t/metaprogramming-surprise/32013>\
**Category:** ruby-talk\
**Created:** [17 October 2006 16:16 UTC](https://rubytalk.org/t/metaprogramming-surprise/32013 "2006-10-17T16:16:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gavin\_Kistner](https://avatars.discourse-cdn.com/v4/letter/g/ed655f/32.png) [@Gavin\_Kistner](https://rubytalk.org/u/Gavin_Kistner)\
**Post date:** [17 October 2006 16:16 UTC](https://rubytalk.org/t/metaprogramming-surprise/32013/1 "2006-10-17T16:16:51Z")

</div>

Artur Merke wrote:

> class A  
> &nbsp;&nbsp;&nbsp;@@class\_variable  
> &nbsp;&nbsp;&nbsp;@class\_instance\_variable  
> &nbsp;&nbsp;&nbsp;def initialize  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@instance\_variable  
> &nbsp;&nbsp;&nbsp;end  
> end
> 
> I hope this is the correct naming for this variables

That's the way I think of them, too. It might help you to remember that  
instance variables are just instance variables, regardless of where they  
are stored. "Class instance variables" are just normal instance  
variables that happen to be stored on a Class object. Nothing special  
about them.

---

<div class="post-metadata">

**Author:** ![J2M](https://avatars.discourse-cdn.com/v4/letter/j/e79b87/32.png) [@J2M](https://rubytalk.org/u/J2M)\
**Post date:** [17 October 2006 17:20 UTC](https://rubytalk.org/t/metaprogramming-surprise/32013/2 "2006-10-17T17:20:12Z")

</div>

To achieve what you were trying you may want to look at the class  
extensions in rails where there are simple class level accessors and  
inheritable\_class accessors that give each inherited class it's own  
copy of the accessors from the parent class.

[http://dev.rubyonrails.org/svn/rails/branches/stable/activesupport/lib/active\_support/core\_ext/class/](http://dev.rubyonrails.org/svn/rails/branches/stable/activesupport/lib/active_support/core_ext/class/)

---

<div class="post-metadata">

**Author:** ![Ara.T.Howard6](https://avatars.discourse-cdn.com/v4/letter/a/958977/32.png) [@Ara.T.Howard6](https://rubytalk.org/u/Ara.T.Howard6)\
**Post date:** [17 October 2006 17:33 UTC](https://rubytalk.org/t/metaprogramming-surprise/32013/3 "2006-10-17T17:33:24Z")

</div>

attributes.rb is 42 lines of code and does all of that too, plus up front or  
deferred initialisation.

&nbsp;&nbsp;&nbsp;harp:~ \> cat a.rb  
&nbsp;&nbsp;&nbsp;require 'rubygems'  
&nbsp;&nbsp;&nbsp;require 'attributes'

&nbsp;&nbsp;&nbsp;class A  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;class \<\< self  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;attribute 'a' =\> 42  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;attribute('classname'){ self.name }  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;class B \< A  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;p A.a  
&nbsp;&nbsp;&nbsp;p B.a

&nbsp;&nbsp;&nbsp;p A.classname  
&nbsp;&nbsp;&nbsp;p B.classname

&nbsp;&nbsp;&nbsp;harp:~ \> ruby a.rb  
&nbsp;&nbsp;&nbsp;42  
&nbsp;&nbsp;&nbsp;"A"  
&nbsp;&nbsp;&nbsp;"B"

-a

> **···**
>
> On Wed, 18 Oct 2006, J2M wrote:
> 
> > To achieve what you were trying you may want to look at the class  
> > extensions in rails where there are simple class level accessors and  
> > inheritable\_class accessors that give each inherited class it's own  
> > copy of the accessors from the parent class.
> > 
> > [http://dev.rubyonrails.org/svn/rails/branches/stable/activesupport/lib/active\_support/core\_ext/class/](http://dev.rubyonrails.org/svn/rails/branches/stable/activesupport/lib/active_support/core_ext/class/)
> 
> --  
> my religion is very simple. my religion is kindness. -- the dalai lama
