From: "Enric Lafont" <enric / 1smart.com>


> Paul E.C. Melis wrote:
>
> This brings another question, why does ruby use as default an instance
> method (initialize) for doing what must be a class method (new). I think
> that the current approach, does not follow the least surprise principle.

When you perform "x = Froz.new", you're not calling Froz.new, you're callnig
Class.new, because Froz.is_a? Class.

Class.new performs some groundwork for creating an object, and then hands over
to Froz.initialize.  :initialize does exactly what its name says: it
initialises the object.  It couldn't be a class method: it needs to access
instance data.

It may be restrictive, or not, but you do need some protocol.

> You can of course do:
>
> class Test
>   def Test.new_from_file('someFile')
>      aTempVariable=Test.new   # because it seems to be the only
> contructor allowed
>     # ... whatever other things this routine does
>      return aTempVariable
>    end
> end
>
> This way you can  then
>
> aTestVar = Test.new_from_file('someFile')
>
> But it's unnatural and strange (at least for me ...)

Not for me.  Just rename to method to from_file, and the client code reads

  test_var = Test.from_file('some_file')

Not too bad, is it?  I would go for Test.new('file') (I'd probably use a File
object, actually).  Anyway, there's plenty of precendent:

  Time.now
  Time.local
  Thread.current
  ...

> Enric

Gavin