B
Bret Pettichord
In several cases, i find myself wanting to create constants for a set of
related classes. Each class has a different value, defined once, and then
have the methods of the "abstract" class be able to reference the "classes"
value.
I've tried doing this thing with methods and class variables and class
constants, but the only one that works is methods. Like support_types in
the following code from Watir:
class TextField < Element
def locate
if @how == :from_object
ole_object = @what
else
ole_object = @container.getObject(@how, @what,
supported_types)
end
@o = ole_object
end
def supported_types
return ["text", "password", "textarea"]
end
private :supported_types
...
class Hidden < TextField
def initialize(container, how, what)
super
end
def supported_types
return ["hidden"]
end
I've found that the only way to get the behavior that i want is to use
these kinds of "constant" methods. These seems inelegant, and i am
wondering if there is a cleaner way of doing this kind of thing?
Here's another example of where i've done this kind of thing:
class SelectLists < ElementCollections
include CommonCollection
def element_class; SelectList; end
def element_tag; 'SELECT'; end
end
Any suggestions. If i recall correctly, class constants didn't work because
it wouldn't get the right scope. And class variables didn't have separate
values for each of the classes.
Anyway, it seemed kludgy to have to use a method, when a constant or
variable would do. Comments?
Bret
_____________________
Bret Pettichord
www.pettichord.com
related classes. Each class has a different value, defined once, and then
have the methods of the "abstract" class be able to reference the "classes"
value.
I've tried doing this thing with methods and class variables and class
constants, but the only one that works is methods. Like support_types in
the following code from Watir:
class TextField < Element
def locate
if @how == :from_object
ole_object = @what
else
ole_object = @container.getObject(@how, @what,
supported_types)
end
@o = ole_object
end
def supported_types
return ["text", "password", "textarea"]
end
private :supported_types
...
class Hidden < TextField
def initialize(container, how, what)
super
end
def supported_types
return ["hidden"]
end
I've found that the only way to get the behavior that i want is to use
these kinds of "constant" methods. These seems inelegant, and i am
wondering if there is a cleaner way of doing this kind of thing?
Here's another example of where i've done this kind of thing:
class SelectLists < ElementCollections
include CommonCollection
def element_class; SelectList; end
def element_tag; 'SELECT'; end
end
Any suggestions. If i recall correctly, class constants didn't work because
it wouldn't get the right scope. And class variables didn't have separate
values for each of the classes.
Anyway, it seemed kludgy to have to use a method, when a constant or
variable would do. Comments?
Bret
_____________________
Bret Pettichord
www.pettichord.com