Traversing superviews' class instance variables

M

Mike Austin

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 = [:eek:rigin]
def View.fields()
super.fields()
@fields
end
attr :eek:rigin
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
 
R

Ross Bamford

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 = [:eek:rigin]
def View.fields()
super.fields()
@fields
end
attr :eek:rigin
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?

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...
 
M

Mike Austin

Ross said:
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 = [:eek:rigin]
def View.fields()
super.fields()
@fields
end
attr :eek:rigin
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?

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 = [:eek:rigin, :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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,667
Latest member
DaniloB294

Latest Threads

Top