Initialize returning nil if error condition - how

Discussion in 'Ruby' started by Graham Nicholls, Jul 6, 2004.

  1. 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, Jul 6, 2004
    #1
    1. Advertising

  2. 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"
    Graham Nicholls, Jul 6, 2004
    #2
    1. Advertising

  3. "Graham Nicholls" <> schrieb im Newsbeitrag
    news:40ea5761$0$15264$...
    > 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
    Robert Klemme, Jul 6, 2004
    #3
  4. Graham Nicholls

    Guest

    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
    , Jul 6, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brian Candler

    puts nil generates "nil\n"

    Brian Candler, Nov 6, 2004, in forum: Ruby
    Replies:
    1
    Views:
    111
  2. John Carter
    Replies:
    64
    Views:
    638
    Klaus Stein
    May 19, 2005
  3. ako...

    a == nil or a.nil?

    ako..., Nov 22, 2005, in forum: Ruby
    Replies:
    6
    Views:
    141
    Douglas Livingstone
    Nov 23, 2005
  4. Christoffer Sawicki
    Replies:
    5
    Views:
    249
    Christoffer Sawicki
    Sep 2, 2006
  5. Eero Saynatkari

    nil.to_s != "nil"

    Eero Saynatkari, Oct 27, 2006, in forum: Ruby
    Replies:
    11
    Views:
    231
    Gregory Brown
    Oct 28, 2006
Loading...

Share This Page