bachase / gmail.com <bachase / gmail.com> wrote:
> Basically, I want the base class to provide
> some default settings that a child class could override using a class
> method. Apparently, I am unclear on the way to do so.
I guess I'm wondering: does it absolutely have to be a class method?
Could it be an ordinary instance method? In this approach, as you
subclass you get to define an instance method, "setup", that you know
will be called by the constructor:
###
class BigGuy
def setup
@favorites = "bluto"
end
def initialize
setup
p @favorites
end
end
class LittleGuy < BigGuy
def setup
@favorites = "popeye"
end
end
BigGuy.new #=> "bluto"
LittleGuy.new #=> "popeye"
BigGuy.new #=> "bluto"
LittleGuy.new #=> "popeye"
###
If you really do want it to be class-based, perhaps a class method might
generate a subclass, set up the way you want it, from which you derive
further subclasses. I'm not very adept at this sort of thing, but my
first go would be something like this:
###
class BigGuy
def setup
@favorites = "bluto"
end
def initialize
setup
p @favorites
end
class MiddleGuy < BigGuy
end
def BigGuy.make_subclass(what)
MiddleGuy.class_eval %{
def setup
@favorites = "#{what}"
end
}
return MiddleGuy
end
end
class LittleGuy < BigGuy.make_subclass('popeye')
end
BigGuy.new #=> "bluto"
LittleGuy.new #=> "popeye"
BigGuy.new #=> "bluto"
LittleGuy.new #=> "popeye"
###
I like the ultimate syntax used to generate LIttleGuy, but I'm not
entirely happy because it assumes that the parameter is a string. But I
got all caught up the "eval" situation and couldn't think straight any
more after a while... Maybe it was a silly idea. m.
--
matt neuburg, phd = matt / tidbits.com, http://www.tidbits.com/matt/
Tiger - http://www.takecontrolbooks.com/tiger-customizing.html
AppleScript - http://www.amazon.com/gp/product/0596102119
Read TidBITS! It's free and smart. http://www.tidbits.com