From: James Edward Gray II Date: 2006-12-03T08:39:12+09:00 Subject: Re: Factory pattern, abstract base class On Dec 2, 2006, at 5:15 PM, Daniel Berger wrote: > Perhaps my whole approach is wrong. What is the proper way to achieve > what I'm after (without resorting to making Bar a module)? This works, but is probably poor style: module Foo class Bar def initialize(arg) end class << self alias_method :__new__, :new def new(arg) return if self != Foo::Bar case arg.downcase when 'baz' return Baz.__new__(arg) when 'zap' return Zap.__new__(arg) end end end end class Baz < Bar; end class Zap < Bar; end end fb = Foo::Bar.new('baz') # => # __END__ I prefer somthing like this though: module Foo class Bar def self.factory(arg) return if self != Foo::Bar Foo.const_get(arg.capitalize).new(arg) end def initialize(arg) end end class Baz < Bar; end class Zap < Bar; end end fb = Foo::Bar.factory('baz') # => # __END__ Hope that helps. James Edward Gray II