# Inheriting class-based state

**URL:** <https://www.ruby-forum.com/t/inheriting-class-based-state/60371>\
**Category:** Ruby\
**Created:** [May 12, 2006, 6:57pm UTC](https://www.ruby-forum.com/t/inheriting-class-based-state/60371 "2006-05-12T18:57:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnlester](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e480ec/32.png) [@johnlester](https://www.ruby-forum.com/u/johnlester)\
**Post date:** [May 12, 2006, 6:57pm UTC](https://www.ruby-forum.com/t/inheriting-class-based-state/60371/1 "2006-05-12T18:57:09Z")

</div>

This one puzzles me - I know there’s something fundamental that I’m not  
understanding about Ruby’s object model. Consider these classes:

class Base  
def self.my\_name=(value)  
@my\_name = value  
end  
def self.my\_name  
@my\_name  
end  
end

class Derived \< Base; end

Base.my\_name = ‘John’  
puts Base.my\_name  
John

Derived.my\_name  
nil  
Derived.my\_name = ‘Mike’  
puts Derived.my\_name  
Mike

So the class instance member @my\_name isn’t inherited by the Derived  
class.

How do I share state between a derived class and its base class?

Thanks  
-John

> **[John Lam's Blog](https://www.iunknown.com/)**
>
> Since 1997

---

<div class="post-metadata">

**Author:** ![johnlester](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e480ec/32.png) [@johnlester](https://www.ruby-forum.com/u/johnlester)\
**Post date:** [May 12, 2006, 7:10pm UTC](https://www.ruby-forum.com/t/inheriting-class-based-state/60371/2 "2006-05-12T19:10:20Z")

</div>

@ means instance member variable  
@@ means class member variable

Just change @my\_name to @@my\_name and you’ll be good to go.

David K.

---

<div class="post-metadata">

**Author:** ![johnlester](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e480ec/32.png) [@johnlester](https://www.ruby-forum.com/u/johnlester)\
**Post date:** [May 12, 2006, 7:25pm UTC](https://www.ruby-forum.com/t/inheriting-class-based-state/60371/3 "2006-05-12T19:25:56Z")

</div>

John L. wrote:

> end  
> puts Derived.my\_name  
> Mike
> 
> So the class instance member @my\_name isn’t inherited by the Derived class.

That’s right. The #my\_name and #my\_name= methods are inherited, but when  
you call them on Derived, they operate on the instance variables of  
Derived.

> How do I share state between a derived class and its base class?

Well, there are “class variables”, written @@my\_name. But they can have  
some surprising behaviors in current ruby(\*), and in future ruby they  
will not be shared up and down the inheritance hierarchy (they will only  
be shared between a class and its instances).

An alternative:

class A  
class \<\< self  
attr\_accessor :\_\_my\_name

```
def my_name; A.__my_name; end
def my_name=(n); A.__my_name = n; end

```

end  
end

class B \< A  
end

B.my\_name = “fred”

p A.my\_name # ==\> “fred”

Or, if you don’t want \_\_my\_name polluting the namespace, you could use  
this:

class A  
class \<\< self  
def my\_name; A.instance\_variable\_get(:@my\_name); end  
def my\_name=(n); A.instance\_variable\_set(:@my\_name, n); end  
end  
end

class B \< A  
end

B.my\_name = “fred”

p A.my\_name

The key difference between these two examples and your original attempt  
is that, here, the “A” reference binds _statically_ to the class A,  
whereas, in the original code, the “@my\_name” reference binds  
_dynamically_ to the instance on which my\_name is called (which might  
turn out to be A, or B or …).

HTH.

(\*) search the archives–briefly, if you assign to a subclass’s @@x, and  
then assign to the superclass’s @x you get two different variables.  
Reverse the order of assignment, and there is only one variable:

irb(main):014:0\> class A; end  
=\> nil  
irb(main):015:0\> class B\<A; end  
=\> nil  
irb(main):016:0\> class A; @@x=1; end  
=\> 1  
irb(main):017:0\> class B; p @@x; end  
1  
=\> nil  
irb(main):018:0\> class B; @@y = 2; end  
=\> 2  
irb(main):019:0\> class A; p @@y; end  
NameError: uninitialized class variable @@y in A  
from (irb):19  
irb(main):020:0\> class A; @@y = 3; end  
=\> 3  
irb(main):021:0\> class B; p @@y; end  
2  
=\> nil

---

<div class="post-metadata">

**Author:** ![johnlester](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e480ec/32.png) [@johnlester](https://www.ruby-forum.com/u/johnlester)\
**Post date:** [May 12, 2006, 7:44pm UTC](https://www.ruby-forum.com/t/inheriting-class-based-state/60371/4 "2006-05-12T19:44:52Z")

</div>

Thanks, Joel - this is exactly what I needed to fix some ugliness in  
RubyCLR.

I was trying to get class variables to work earlier, and actually ran  
across  
the problem that you mentioned in your (\*). I just didn’t know it at the  
time 🙂

Cheers,  
-John

> **[John Lam's Blog](https://www.iunknown.com/)**
>
> Since 1997

---

<div class="post-metadata">

**Author:** ![johnlester](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e480ec/32.png) [@johnlester](https://www.ruby-forum.com/u/johnlester)\
**Post date:** [May 12, 2006, 7:51pm UTC](https://www.ruby-forum.com/t/inheriting-class-based-state/60371/5 "2006-05-12T19:51:14Z")

</div>

On Sat, 13 May 2006, Joel VanderWerf wrote:

> p A.my\_name # ==\> “fred”  
> one can just use traits for this:

harp:~ \> gem install traits

harp:~ \> cat a.rb  
require ‘traits’  
class A; class\_trait ‘my\_name’ =\> ‘fred’; end  
class B \< A; end

p A.my\_name  
p B.my\_name

B.my\_name = ‘joe’

p A.my\_name  
p B.my\_name

harp:~ \> ruby a.rb  
“fred”  
“fred”  
“fred”  
“joe”

voila - best of both worlds - shared **and** overridable values if  
required.

-a
