# Couple of questions from a new user

**URL:** <https://www.ruby-forum.com/t/couple-of-questions-from-a-new-user/109356>\
**Category:** Ruby\
**Created:** [July 13, 2007, 6:46pm UTC](https://www.ruby-forum.com/t/couple-of-questions-from-a-new-user/109356 "2007-07-13T18:46:07Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![benemson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a8b319/32.png) [@benemson](https://www.ruby-forum.com/u/benemson)\
**Post date:** [July 13, 2007, 6:46pm UTC](https://www.ruby-forum.com/t/couple-of-questions-from-a-new-user/109356/1 "2007-07-13T18:46:07Z")

</div>

Hi, ive been using Ruby for a few months at work. Mainly for fairly  
simple scripting (type of thing you used to do in perl).

My first question is about initialize method, for overriding new (I  
believe). If it is in fact overriding new why is it not called new?  
it being called initialize is interesting because this is an American  
spelling and I am a dyslexic person from the UK so you can imagine the  
fun I am having;)

My second question is about TestUnit. I put a class inherited from  
Test::Unit::TestCase at the end of my .rb file and it automatically  
run the tests. This is cool but how did it know to run the class, I  
did not tell it. I know ruby is cunning but is it reading my mind;).  
This reminds me of the language called z we were talking about in the  
80s. It only had one command, which was.

“Do what I want you to do and tell me when you are finished”.

Regards,  
Ben

---

<div class="post-metadata">

**Author:** ![benemson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a8b319/32.png) [@benemson](https://www.ruby-forum.com/u/benemson)\
**Post date:** [July 13, 2007, 7:00pm UTC](https://www.ruby-forum.com/t/couple-of-questions-from-a-new-user/109356/2 "2007-07-13T19:00:09Z")

</div>

On 7/13/07, Ben E. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi, ive been using Ruby for a few months at work. Mainly for fairly  
> simple scripting (type of thing you used to do in perl).
> 
> My first question is about initialize method, for overriding new (I  
> believe). If it is in fact overriding new why is it not called new?  
> it being called initialize is interesting because this is an American  
> spelling and I am a dyslexic person from the UK so you can imagine the  
> fun I am having;)

Initialize does not override new, it is invoked by new().

The issue is that when you do Something.new, no instance exists yet.  
However, initialize is called _after_ the instance has been created,  
which allows it to run in the context of that object.

I believe it is possible to override new with some hackery, but you  
usually won’t need that, initialize will do.

> My second question is about TestUnit. I put a class inherited from  
> Test::Unit::TestCase at the end of my .rb file and it automatically  
> run the tests. This is cool but how did it know to run the class, I  
> did not tell it. I know ruby is cunning but is it reading my mind;).  
> This reminds me of the language called z we were talking about in the  
> 80s. It only had one command, which was.

Test::Unit uses (I believe) ObjectSpace to look up all of it’s  
subclasses and then run them. I don’t know the exact hooks that are  
called, but that’s the general approach.

If you want to embed tests at the bottom of a file, you might use this  
idiom

if **FILE** == $PROGRAM\_NAME  
#define tests here  
end

this way, when you run that script, it will run the tests, but if you  
require it from another file, the tests won’t run, allowing it to be  
used as a library.

Warm Regards,  
-gregory
