# Getting initialization status from "foo = Class.new"

**URL:** <https://www.ruby-forum.com/t/getting-initialization-status-from-foo-class-new/75742>\
**Category:** Ruby\
**Created:** [November 13, 2006, 2:22am UTC](https://www.ruby-forum.com/t/getting-initialization-status-from-foo-class-new/75742 "2006-11-13T02:22:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![r0dzilla](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c4cdca/32.png) [@r0dzilla](https://www.ruby-forum.com/u/r0dzilla)\
**Post date:** [November 13, 2006, 2:22am UTC](https://www.ruby-forum.com/t/getting-initialization-status-from-foo-class-new/75742/1 "2006-11-13T02:22:35Z")

</div>

Here’s a ruby noob question for ya:

I have a class I’ve made that the initalize method searches for a file  
needed for the class to function.

Right now, I’ve made a class variable, “found”, that is set to true or  
false if the initialize method could or could not find the file.

For example:

foo = Class.new

if foo.found == true

# continue program

else

# give error message

exit

Is there a way for “Class.new” to signal failure? I originally had the  
class’s initialize method return true or false but this doesn’t seem to  
be reflected in a “foo = Class.new” when trying it in irb.

Using the class variable “found” works but shouldn’t Class.new signal  
failure?

---

<div class="post-metadata">

**Author:** ![r0dzilla](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c4cdca/32.png) [@r0dzilla](https://www.ruby-forum.com/u/r0dzilla)\
**Post date:** [November 13, 2006, 2:33am UTC](https://www.ruby-forum.com/t/getting-initialization-status-from-foo-class-new/75742/2 "2006-11-13T02:33:44Z")

</div>

On 11/13/06, Rodney B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Is there a way for “Class.new” to signal failure? I originally had the  
> class’s initialize method return true or false but this doesn’t seem to  
> be reflected in a “foo = Class.new” when trying it in irb.
> 
> Using the class variable “found” works but shouldn’t Class.new signal  
> failure?

How about raising an exception?  
[http://www.rubycentral.com/book/tut\_exceptions.html](http://www.rubycentral.com/book/tut_exceptions.html)

-Harold

---

<div class="post-metadata">

**Author:** ![r0dzilla](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c4cdca/32.png) [@r0dzilla](https://www.ruby-forum.com/u/r0dzilla)\
**Post date:** [November 13, 2006, 3:53am UTC](https://www.ruby-forum.com/t/getting-initialization-status-from-foo-class-new/75742/3 "2006-11-13T03:53:51Z")

</div>

Rodney B. wrote:

> foo = Class.new
> 
> Using the class variable “found” works but shouldn’t Class.new signal  
> failure?

Generally when the initialize method fails it should raise an exception.

---

<div class="post-metadata">

**Author:** ![r0dzilla](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c4cdca/32.png) [@r0dzilla](https://www.ruby-forum.com/u/r0dzilla)\
**Post date:** [November 14, 2006, 11:54am UTC](https://www.ruby-forum.com/t/getting-initialization-status-from-foo-class-new/75742/4 "2006-11-14T11:54:15Z")

</div>

Hi, Rodney,

```
   I wrote a small program for you, maybe it can meet your 

```

requirements.  
I think you intended that your program flow could be controled directly  
by  
“foo = Class.new” instead of other dedicated variables like “@found”  
for  
this purpose.  
//////////////////////program commences///////////////////////  
class ClassAndFileTest  
def initialize  
puts “shiwei zhang”;  
#searching the file…  
#if found the file  
;  
#if did not find the file  
raise “file not found!”;  
end  
end

begin  
foo=ClassAndFileTest.new;  
rescue  
#give error message  
puts $!;  
exit;  
end  
# continue program  
puts “file found!”;  
…  
/////////////////////////program concludes////////////////////////////

Rgds,  
Shiwei
