# Traversing superviews' class instance variables

**URL:** <https://www.ruby-forum.com/t/traversing-superviews-class-instance-variables/55355>\
**Category:** Ruby\
**Created:** [March 20, 2006, 8:00am UTC](https://www.ruby-forum.com/t/traversing-superviews-class-instance-variables/55355 "2006-03-20T08:00:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![exile](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/50afbb/32.png) [@exile](https://www.ruby-forum.com/u/exile)\
**Post date:** [March 20, 2006, 8:00am UTC](https://www.ruby-forum.com/t/traversing-superviews-class-instance-variables/55355/1 "2006-03-20T08:00:52Z")

</div>

I have certain variables that are ‘properties’ and I keep track of them  
in a  
class instance variable called @fields, and each subclass may have it’s  
own  
fields. I want to navigate through the hierarchy and go through each  
list, is  
something like this possible?:

class View  
@fields = [:origin]  
def View.fields()  
super.fields()  
@fields  
end  
attr :origin  
end

class Button \< View  
@fields = [:caption]  
attr :caption  
end

View.fields() actually finds :cation when I do Button.fields(), but  
super does  
not work. Or maybe there is another way to do this?

Thanks,  
Mike

---

<div class="post-metadata">

**Author:** ![exile](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/50afbb/32.png) [@exile](https://www.ruby-forum.com/u/exile)\
**Post date:** [March 20, 2006, 12:18pm UTC](https://www.ruby-forum.com/t/traversing-superviews-class-instance-variables/55355/2 "2006-03-20T12:18:00Z")

</div>

On Mon, 2006-03-20 at 15:58 +0900, Mike A. wrote:

> end

It seems to work if you replace View.fields with:

def View.fields  
@fields + (superclass.respond\_to?(:fields) ? superclass.fields : [])  
end

But this seems like a bit of a strange setup to me…

---

<div class="post-metadata">

**Author:** ![exile](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/50afbb/32.png) [@exile](https://www.ruby-forum.com/u/exile)\
**Post date:** [March 20, 2006, 9:18pm UTC](https://www.ruby-forum.com/t/traversing-superviews-class-instance-variables/55355/3 "2006-03-20T21:18:57Z")

</div>

Ross B. wrote:

> > ```
> > @fields
> > 
> > ```
> > 
> > not work. Or maybe there is another way to do this?
> 
> It seems to work if you replace View.fields with:
> 
> def View.fields  
> @fields + (superclass.respond\_to?(:fields) ? superclass.fields : [])  
> end
> 
> But this seems like a bit of a strange setup to me…

Thanks, that works. It is strange, but it’s the best way I see to  
classify  
instance variables as being ‘public properties’ or not. Here’s an  
example of  
how it will be used:

class View  
@fields = [:origin, :extent]  
[…]

def View.fields()  
@fields + (superclass != Object ? superclass.fields : [])  
end

def View.inherited( subclass )  
subclass.class\_eval do  
@fields = [] # Make subclasses have @fields  
end  
end  
end

class Button \< View  
@fields = [:action]  
[…]  
end

puts Button.fields

Have a good one,  
Mike
