Initialize returning nil if error condition - how

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

···

--
With Linux, the answer's always "Yes"

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?
Thanks
Graham

···

--
With Linux, the answer's always "Yes"

Hi,

At Tue, 6 Jul 2004 16:37:49 +0900,
Graham Nicholls wrote in [ruby-talk:105342]:

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:

Default Class#new always returns created instance unless any
exceptions occurred. What you want isn't impossible by your
own "new" class method, but raising an exception in
"initialize" is much better.

  class CoverFile
    def initialize(fname)
    end
    def self.new(*args, &block)
      if obj = allocate()
        obj.__send__(*args, &block)
      end
      obj
    end
  end

···

--
Nobu Nakada

"Graham Nicholls" <graham@rockcons.co.uk> 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