Proxy class / little factory

Hmmm … this seems like a perfect spot for forwardable.rb to me:

require “forwardable”

class Text
extend Forwardable

def initialize(component)
if(component <= 1)
@proxy = TextSingle.new
else
@proxy = TextMultiple.new
end
end

def_delegator(:@proxy, :meth)

class TextSingle
def meth
puts "I’m from TextSingle!"
end
end

class TextMultiple
def meth
puts "I’m from TextMultiple!"
end
end
end

···

At Tue, 27 Aug 2002 12:01:25 +0900, Tom Sawyer wrote:

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