# Class variables in an inheritance hierarchy

**URL:** <https://www.ruby-forum.com/t/class-variables-in-an-inheritance-hierarchy/52935>\
**Category:** Ruby\
**Created:** [January 20, 2006, 5:44pm UTC](https://www.ruby-forum.com/t/class-variables-in-an-inheritance-hierarchy/52935 "2006-01-20T17:44:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![toadkicker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5fc32e/32.png) [@toadkicker](https://www.ruby-forum.com/u/toadkicker)\
**Post date:** [January 20, 2006, 5:44pm UTC](https://www.ruby-forum.com/t/class-variables-in-an-inheritance-hierarchy/52935/1 "2006-01-20T17:44:24Z")

</div>

Assume the following structure:

class Base  
end

class Account \< Base  
end

class Opportunity \< Base  
end

I want to have a class variable Account.fields that is different than  
Opportunity.fields, so that all instances of Account can reference the  
Account.fields and all instances if Opportunity can reference  
Opportunity.fields and get the correct results.

As a reference, I am trying to create something akin to ActiveRecord  
(for a  
different type of datasource) where each distinct object has it’s own  
set of  
attributes. I’ve been looking at the ActiveRecord code, but being new to  
Ruby it’s giving me a bit of a headache 🙂

Thanks!

Todd B.

---

<div class="post-metadata">

**Author:** ![toadkicker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5fc32e/32.png) [@toadkicker](https://www.ruby-forum.com/u/toadkicker)\
**Post date:** [January 20, 2006, 8:18pm UTC](https://www.ruby-forum.com/t/class-variables-in-an-inheritance-hierarchy/52935/2 "2006-01-20T20:18:36Z")

</div>

Hi –

On Sat, 21 Jan 2006, Todd B. wrote:

> I want to have a class variable Account.fields that is different than  
> Opportunity.fields, so that all instances of Account can reference the  
> Account.fields and all instances if Opportunity can reference  
> Opportunity.fields and get the correct results.

Account.fields and Opportunity fields are methods, not variables. You  
can easily define them:

class Account \< Base  
def self.fields  
# code here, possibly using instance variable to hold info  
end

# etc.

end

Or you could put it in a module and extend the various classes with  
it. (The best exact way will depend on what’s in the method.)

For the instances:

class Base  
def fields  
self.class.fields  
end  
end

That way, each object will know to query its own class to get the  
right fields method.

David

–  
David A. Black  
removed\_email\_address@domain.invalid

“Ruby for Rails”, from Manning Publications, coming April 2006!

> **[Ruby for Rails](https://www.manning.com/books/ruby-for-rails)**
>
> NEWER EDITION AVAILABLE
> 
> The Well-Grounded Rubyist, Second Edition is now available. An eBook of the previous edition, The Well-Grounded Rubyist is included at no additional cost when you buy the revised edition! 
> 
> 
> Ruby for Rails helps Rails...

---

<div class="post-metadata">

**Author:** ![toadkicker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5fc32e/32.png) [@toadkicker](https://www.ruby-forum.com/u/toadkicker)\
**Post date:** [January 20, 2006, 8:34pm UTC](https://www.ruby-forum.com/t/class-variables-in-an-inheritance-hierarchy/52935/3 "2006-01-20T20:34:36Z")

</div>

Todd B. wrote:

> Assume the following structure:
> 
> class Base  
> end
> 
> class Account \< Base  
> end
> 
> class Opportunity \< Base  
> end
> 
> I want to have a class variable Account.fields that is different than  
> Opportunity.fields, so that all instances of Account can reference the  
> Account.fields and all instances if Opportunity can reference  
> Opportunity.fields and get the correct results.
> 
> As a reference, I am trying to create something akin to ActiveRecord  
> (for a  
> different type of datasource) where each distinct object has it’s own  
> set of  
> attributes. I’ve been looking at the ActiveRecord code, but being new to  
> Ruby it’s giving me a bit of a headache 🙂

You can use class instance variables (Classes Are Objects Too™ 🙂

class Account \< Base  
# We are in the context of the class so this variable  
# will belong to the class, not its instances.  
@fields = something  
end

You would also need to set up accessors for the variables.

> Thanks!
> 
> Todd B.

E
