From: furufuru / ccsr.u-tokyo.ac.jp (Ryo Furue)
Subject: [ruby-talk:19763] Re: "new" returning nil: how to report the failure of object creation
Date: Wed, 15 Aug 2001 18:31:11 +0900

> My current problem is similar to this case.  My class constructs an
> object depending on data in a file.  The datafile is in this form:
> 
>    non-data
>    ...
>    data begin
>       data
>       data
>       ....
>    data end
>    non-data
>    ...
>    data begin
>     ...
> 
> Only the class knows what constitutes data and which part of the file
> to ignore.  So, only by calling "new" of that class can we know
> whether the remaining part of the file contains enough data.  But, we
> also know that the file is bound to reach its end, so that handling
> that condition is truly a normal part of our program:
> 
>    while s = MyClass.new(file)
>       arr.push(s)
>    end
>    # use arr

I think the only problem with above code is that it breaks a
convention that the class method new returns an instance of that
class.

One solution to this problem is just renaming this factory
method to MyClass.parse or something like that.  The method calls
the new, internally.  You can cross the point by making the new
private.

Best Regards,
Kenichi Komiya.