From: Stefan Mueller Date: 2002-05-01T00:39:09+09:00 Subject: Override "initialize" Hello I've been trying to find a way to prevent anyClass.new from calling initialize. I wondered how Marshal is able to create instances of classes even when initialize requires parameters that are not optional. The only way I could make it work seems rather cumbersome and prone to cause trouble. I'd really appreciate some opinions. -------- module Instance def Instance.append_features(cl) cl.instance_eval %{ def instance Thread.critical = true begin alias_method :__origInit__, :initialize alias_method :initialize, :__newInit__ return new ensure alias_method :__newInit__, :initialize alias_method :initialize, :__origInit__ Thread.critical = false end end } cl.module_eval %{ def __newInit__; end } end end class Foo def initialize raise "prevent instance" end def foo "bar" end end class Foo include Instance end #Foo.new.foo # => RuntimeError puts Foo.instance.foo # => "bar" bye, Stefan Mueller