Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Prevent class instantiation from the initialize method?

Reply
Thread Tools

Prevent class instantiation from the initialize method?

 
 
Dave Mihalik
Guest
Posts: n/a
 
      07-27-2006

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/.

 
Reply With Quote
 
 
 
 
Jake McArthur
Guest
Posts: n/a
 
      07-27-2006
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

 
Reply With Quote
 
 
 
 
Marcin Mielżyński
Guest
Posts: n/a
 
      07-27-2006
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
 
Reply With Quote
 
Matthew Smillie
Guest
Posts: n/a
 
      07-27-2006
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.





 
Reply With Quote
 
Dave Mihalik
Guest
Posts: n/a
 
      07-27-2006

>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/.

 
Reply With Quote
 
Alex Young
Guest
Posts: n/a
 
      07-27-2006
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?
>



 
Reply With Quote
 
Dave Mihalik
Guest
Posts: n/a
 
      07-27-2006
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/.

 
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
Explicit instantiation of STL vector demands explicit instantiation of all the templates it using internally. krunalbauskar@gmail.com C++ 1 12-25-2006 03:51 PM
what are the other ways to prevent a class from being subclassed. one way is to declare the class final. srinivas.veeranki@gmail.com Java 20 02-17-2006 12:21 AM
if instance variable get initialize after assigning some values or after constructor then when does static variable get initialize Tony Morris Java 3 02-04-2006 08:39 AM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
how to prevent component instantiation every time? Jim Hammond ASP .Net 4 11-11-2003 07:49 PM



Advertisments