Proxy class / little factory

Any reason why TextSingle and TextMultiple can’t be subclassed from Text?

···

On Monday 26 August 2002 10:01 pm, Tom Sawyer wrote:

maybe someone can help me. i am trying to make a class such that when it
is used it acts like one or another class based on an attribute. i wrote
this little factory version:

class Text

def new(component)
if component <= 1
return TextSingle.new
else
return TextMultiple.new
end
end

class TextSingle
def initilaize

end
end

class TextMultiple
def initilaize

end
end

end

this could work (i actually haven’t run it) but the problem is that the
type of the object returned is not Text, but TextSingle or TextMultiple.
i need it to be Text. eg.

atext = Text.new(1)
p atext.class # → TextSingle

for it it needs to be:

p atext.class # → Text

suggestions?

~transami

Any reason why TextSingle and TextMultiple can’t be subclassed from Text?

doh! that’s seems the simple answer i missed. then #kind_of(Text) would
still work.

thanks!

and thanks to everyone who responded.

···


~transami