From: Robert Klemme Date: 2005-10-15T20:06:54+09:00 Subject: Re: Factory function like Array() for your own classes Robert Klemme wrote: > Dave Burt wrote: >> Trans: >>> >>> Yep. Yep. Yep. I've sugegsted it before. But some people don't like >>> it, insisting that the ".new" gives some sort of additional clarity. >>> Don't know, but like your (our) way better --even wish it were >>> built-in. >> >> Like this? >> >> def method_missing(sym, *args, &block) >> const = Object.const_get(sym) rescue nil >> if const.kind_of?(Module) >> const.new(*args, &block) >> else >> raise NoMethodError("undefined method '#{sym}' for >> #{self.inspect}") # OK, it is cool. >> end >> end > > I'd prefer > > def method_missing(s, *a, &b) > Object.const_get(s).new(*a,&b) > end > > Ruby will throw exceptions for any cases that don't work anyway. > > Kind regards > > robert PS: Note also the alternative approach of Array[]: >> class Foo >> class <> alias :[] :new >> end >> end => nil >> Foo[] => # Kind regards robert