From: Robert Klemme Date: 2004-07-06T17:37:38+09:00 Subject: Re: Initialize returning nil if error condition - how "Graham Nicholls" schrieb im Newsbeitrag news:40ea5761$0$15264$afc38c87@auth.uk.news.easynet.net... > Graham Nicholls wrote: > > > I've got "The Ruby Way" & "Programming Ruby", but can't find out how to > > have an initializer return nil if theres a problem - something like this: > > > > Class Cover_file > > def initialize(fname) > > 64 @fname=fname > > 65 if $verbose > > 66 printf("Checking file %s\n",@fname) > > 67 end > > 68 # Parse filename for component parts > > (product,policy_no,site_no,schedule) > > 69 comp_patt=Regexp.compile(/^([A-Z]+)([0-9]+)([0-9][0-9] > > ([\w\d]+)\.en$/) > > 70 if @fname !~ comp_patt > > 71 if $verbose > > 72 printf("Sorry, %s does not match cover clause filename > > template\n",fname) > > 73 end > > 74 return nil > > > > If I try self=nil, I (not unreasonably - I expected it) get an error. > > > > What I want to do is this: > > > > cfile=Cover_file.new(fname) > > if cfile == nil > > some error handling > > end > > > > But I can't work out how. > > Thanks. > > Graham Nicholls > > > Just had an idea (thats the 2nd time I've done that this week! (not had an > idea, fools, commented on my own post!). I'm supposed to raise an error, > aren't I? Yes, that's the proper way to deal it. Exceptions make code much cleaner if properly used. Regards robert