From: Avi Bryant Date: 2001-08-15T13:02:26+09:00 Subject: [ruby-talk:19757] Re: "new" returning nil: how to report the failure of object creation On Wed, 15 Aug 2001, Ryo Furue wrote: > Hi there, > > How do you report the failure of object creation, that is, if you find > it impossible to properly initialize the object in the "initialize" > method? Raising an exception is clearly an option. But, what if the > "failure" is a normal part of the flow of the program? as in > > class SomeClass > def initialize(file) > line = file.gets > if line > # initialize instance vars according to "line" > else > return nil ##<- a fictious Ruby feature > end > end > end The easiest way is to override new instead; ie, something like: class SomeClass def self.new(file) line = file.gets if line super(line) else nil end end def initialize(line) #initialize object end end avi