From: Dave Burt Date: 2005-10-15T12:01:58+09:00 Subject: Re: Factory function like Array() for your own classes Sean O'Halpin: > I was quite surprised today to find out that you can define both a > class and a function with the same name and still be able to reference > them both separately. > > This makes it possible to define factory functions that work like the > built-in Array() and String() functions, e.g. so that Thing(1,2,3) is > the same as Thing.new(1,2,3). > ... > So... what do you think? In Ruby, so we're told, methods begin with a lower-case letter. Now, actually, you can have all kinds of funky names: irb> class Object irb> define_method("!@#%$") { 43 } irb> end => # irb> send("!@#%$") 43 I think we don't often see these is because methods _should_ begin in lower-case. That said, as Ara has pointed out, there are 4 such methods in the core, and at least one more in the standard library (Rational()). But these methods are all cast methods - you feed them something that isn't an Array, Integer, Float, String, Rational, and it gives you either an object of the correct type or an error. I suppose that part of your Thing example could be seen as a cast from a hash to a Thing, but taking a block is a bit of a stretch. Why not just use ..new? Why does new need an alias? Cheers, Dave