# Re: metaprogramming surprise?

**URL:** <https://www.ruby-forum.com/t/re-metaprogramming-surprise/72503>\
**Category:** Ruby\
**Created:** [October 17, 2006, 6:17pm UTC](https://www.ruby-forum.com/t/re-metaprogramming-surprise/72503 "2006-10-17T18:17:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![phrogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@phrogz](https://www.ruby-forum.com/u/phrogz)\
**Post date:** [October 17, 2006, 6:17pm UTC](https://www.ruby-forum.com/t/re-metaprogramming-surprise/72503/1 "2006-10-17T18:17:26Z")

</div>

Artur M. wrote:

> class A  
> @@class\_variable  
> @class\_instance\_variable  
> def initialize  
> @instance\_variable  
> 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:** ![phrogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@phrogz](https://www.ruby-forum.com/u/phrogz)\
**Post date:** [October 17, 2006, 7:21pm UTC](https://www.ruby-forum.com/t/re-metaprogramming-surprise/72503/2 "2006-10-17T19:21:45Z")

</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:** ![phrogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@phrogz](https://www.ruby-forum.com/u/phrogz)\
**Post date:** [October 17, 2006, 7:33pm UTC](https://www.ruby-forum.com/t/re-metaprogramming-surprise/72503/3 "2006-10-17T19:33:50Z")

</div>

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/)

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

harp:~ \> cat a.rb  
require ‘rubygems’  
require ‘attributes’

class A  
class \<\< self  
attribute ‘a’ =\> 42  
attribute(‘classname’){ self.name }  
end  
end

class B \< A  
end

p A.a  
p B.a

p A.classname  
p B.classname

harp:~ \> ruby a.rb  
42  
42  
“A”  
“B”

-a
