Prevent class instantiation from the initialize method?

Discussion in 'Ruby' started by Dave Mihalik, Jul 27, 2006.

  1. Dave Mihalik

    Dave Mihalik Guest

    Can some show me how to implement this java program using Ruby?
    I am simply trying to prevent the testclass from being instantiated in
    the constructor.
    I am then checking to see if the the class has instanatiated properly.

    class testclass
    {
    public testclass() throws Exception
    {

    Exception testexception;
    testexception = new Exception("Instantiation cancelled.");
    throw testexception;

    }}

    public class testprogram
    {
    public static void main(final String taArgv[])
    {
    testclass mytest = null;

    try
    {
    mytest = new testclass();
    }
    catch(Exception myexception)
    {
    System.out.println(myexception.getMessage());
    }

    if (mytest == null)
    {
    System.exit(1);

    }
    System.out.println("Instantiation not cancelled.");
    }}


    I am new to Ruby but this is what I have so far:

    class Testclass
    def initialize()

    raise "Instantiation cancelled."

    end
    end

    begin

    mytest = Testclass.new()

    rescue Exception => e
    begin

    puts e
    exit 1

    end

    puts "Instantiation not cancelled."


    end

    If the raise line in the initialize method is disabled, the puts
    "Instantiation not cancelled." line does not execute.

    --
    Posted via http://www.ruby-forum.com/.
    Dave Mihalik, Jul 27, 2006
    #1
    1. Advertising

  2. On Jul 27, 2006, at 2:21 PM, Dave Mihalik wrote:

    > Can some show me how to implement this java program using Ruby?
    > I am simply trying to prevent the testclass from being instantiated in
    > the constructor.
    > I am then checking to see if the the class has instanatiated properly.


    This seems like a bad idea. I can think of no good reason to do this.
    This is probably just a bad solution to a very different problem.
    What exactly are you planning to do with this?

    - Jake McArthur
    Jake McArthur, Jul 27, 2006
    #2
    1. Advertising

  3. Dave Mihalik wrote:

    class Testclass
    def initialize()

    raise "Instantiation cancelled."

    end
    end


    begin
    mytest = Testclass.new()
    rescue Exception => e

    puts e
    exit 1

    end

    puts "Instantiation not cancelled."


    rescue/ensure must have it's own begin clause

    lopex
    Marcin Mielżyński, Jul 27, 2006
    #3
  4. On Jul 27, 2006, at 20:21, Dave Mihalik wrote:

    >
    > Can some show me how to implement this java program using Ruby?
    > I am simply trying to prevent the testclass from being instantiated in
    > the constructor.
    > I am then checking to see if the the class has instanatiated properly.
    >
    > [Java.elide()]
    >
    > I am new to Ruby but this is what I have so far:
    >
    > class Testclass
    > def initialize()
    >
    > raise "Instantiation cancelled."
    >
    > end
    > end
    >
    > begin
    >
    > mytest = Testclass.new()
    >
    > rescue Exception => e
    > begin
    >
    > puts e
    > exit 1
    >
    > end
    >
    > puts "Instantiation not cancelled."
    >
    >
    > end
    >
    > If the raise line in the initialize method is disabled, the puts
    > "Instantiation not cancelled." line does not execute.


    Because it's always going to execute that inner begin...end block. I
    expect you want something more like the following:

    begin
    mytest = Testclass.new
    rescue Exception => e
    puts e
    exit 1
    end
    puts "Instantiation not cancelled"

    The first 'begin' starts the protected block, similar to Java's try
    { ... }, and the 'rescue' acts on any exceptions raised within that
    block.

    That said, using exceptions for control flow is usually a bit of
    pathological case in Ruby, and there's usually a better way to
    accomplish the same result. The two possible answers that spring to
    mind are (simple) making the initialiser private or (slightly more
    complicated) just removing the new method all together:

    class Testclass
    class << self
    undef_method :new
    end
    end

    my_test = Testclass.new()
    NoMethodError: undefined method `new' for Foo:Class
    from (irb):6
    from :0



    matthew smillie.
    Matthew Smillie, Jul 27, 2006
    #4
  5. Dave Mihalik

    Dave Mihalik Guest


    >That said, using exceptions for control flow is usually a bit of
    > pathological case in Ruby, and there's usually a better way to
    > accomplish the same result. The two possible answers that spring to
    > mind are (simple) making the initialiser private or (slightly more
    > complicated) just removing the new method all together:



    I am trying to determine how my program should handle errors in the
    initialize method. If an error occurs in code that is running in the
    initialize method, I want to be able to prevent the class from
    instantiating.


    class Testclass
    def initialize()

    ## Do work here.
    ## Do more work here.
    ## Get an error from doing more work.

    ## Raise an error to prevent the class from instantiating because
    ## the functionality of the class depends on the above work.

    ##raise "Instantiation cancelled."

    end
    end

    ## Try to instantiate the class.
    begin

    mytest = Testclass.new()

    ## Make sure that the instantiation was successful.
    rescue Exception => e

    ## If not, handle the error.
    puts e
    exit 1

    end

    ## If instantiation succeeded, then proceed with the program.
    puts "Instantiation not cancelled."

    Is there a better way to handle errors in the initialize method?

    --
    Posted via http://www.ruby-forum.com/.
    Dave Mihalik, Jul 27, 2006
    #5
  6. Dave Mihalik

    Alex Young Guest

    Dave Mihalik wrote:
    <snip>
    > I am trying to determine how my program should handle errors in the
    > initialize method. If an error occurs in code that is running in the
    > initialize method, I want to be able to prevent the class from
    > instantiating.

    <snip pseudocode>

    It depends on the nature of the code that can fail, and what *exactly*
    you mean by "instantiating".

    This might work, if the failable code is relevant outside an instance:

    class TestClass
    def TestClass.get_new(params)
    begin
    settings = do_work_that_can_fail(params)
    new(settings)
    rescue
    puts "Waaah! It didn't work!"
    nil
    end
    end
    end

    If that's not appropriate, then there's this:

    class TestClass
    def TestClass.get_new(params)
    begin
    new(params)
    rescue
    puts "Still didn't work!"
    nil
    end
    end
    def initialize(params)
    do_work_that_can_fail(params)
    end
    end

    In the second case (unless I'm mis-remembering how the object lifecycle
    works) the instance is created in the new() call, but it'll get disposed
    of in the next GC round because there aren't any references to it being
    held anywhere.

    In both cases, you just call TestClass.get_new() rather than
    TestClass.new() to get your instance, or nil if the stuff that can fail
    fails.

    --
    Alex

    >
    >
    > class Testclass
    > def initialize()
    >
    > ## Do work here.
    > ## Do more work here.
    > ## Get an error from doing more work.
    >
    > ## Raise an error to prevent the class from instantiating because
    > ## the functionality of the class depends on the above work.
    >
    > ##raise "Instantiation cancelled."
    >
    > end
    > end
    >
    > ## Try to instantiate the class.
    > begin
    >
    > mytest = Testclass.new()
    >
    > ## Make sure that the instantiation was successful.
    > rescue Exception => e
    >
    > ## If not, handle the error.
    > puts e
    > exit 1
    >
    > end
    >
    > ## If instantiation succeeded, then proceed with the program.
    > puts "Instantiation not cancelled."
    >
    > Is there a better way to handle errors in the initialize method?
    >
    Alex Young, Jul 28, 2006
    #6
  7. Dave Mihalik

    Dave Mihalik Guest

    Jake McArthur wrote:
    > On Jul 27, 2006, at 2:21 PM, Dave Mihalik wrote:
    >
    >> Can some show me how to implement this java program using Ruby?
    >> I am simply trying to prevent the testclass from being instantiated in
    >> the constructor.
    >> I am then checking to see if the the class has instanatiated properly.

    >
    > This seems like a bad idea. I can think of no good reason to do this.
    > This is probably just a bad solution to a very different problem.
    > What exactly are you planning to do with this?
    >
    > - Jake McArthur


    I am trying to handle any errors that may occur in the initialize
    method. I don't want to the class to instantiate if errors occur in the
    initialize method.

    --
    Posted via http://www.ruby-forum.com/.
    Dave Mihalik, Jul 28, 2006
    #7
    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. Jim Hammond
    Replies:
    4
    Views:
    398
    Jim Hammond
    Nov 11, 2003
  2. Replies:
    1
    Views:
    572
    Salt_Peter
    Dec 25, 2006
  3. Ed
    Replies:
    1
    Views:
    343
  4. Xeno Campanoli
    Replies:
    9
    Views:
    143
    Aaron Patterson
    Feb 14, 2010
  5. Joseph L. Casale

    Argparse class method based instantiation

    Joseph L. Casale, Jan 2, 2014, in forum: Python
    Replies:
    0
    Views:
    100
    Joseph L. Casale
    Jan 2, 2014
Loading...

Share This Page