Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Initialize returning nil if error condition - how

Reply
Thread Tools

Initialize returning nil if error condition - how

 
 
Graham Nicholls
Guest
Posts: n/a
 
      07-06-2004
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"
 
Reply With Quote
 
 
 
 
Graham Nicholls
Guest
Posts: n/a
 
      07-06-2004
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"
 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      07-06-2004

"Graham Nicholls" <> schrieb im Newsbeitrag
news:40ea5761$0$15264$ t.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

 
Reply With Quote
 
nobu.nokada@softhome.net
Guest
Posts: n/a
 
      07-06-2004
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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Integer(nil) versus Float(nil) versus String(nil) Christoffer Sawicki Ruby 5 09-02-2006 06:28 PM
a == nil or a.nil? ako... Ruby 6 11-23-2005 05:03 AM
RCR 303: nil should accept missing methods and return nil John Carter Ruby 64 05-19-2005 12:12 PM
puts nil generates "nil\n" Brian Candler Ruby 1 11-06-2004 01:59 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57