Initialize returning nil if error condition - how

  • Thread starter Graham Nicholls
  • Start date
G

Graham Nicholls

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
 
G

Graham Nicholls

Graham said:
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
 
R

Robert Klemme

Graham Nicholls said:
Graham said:
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
 
N

nobu.nokada

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top