Couple of questions from a new user

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

On 7/13/07, Ben E. [email protected] 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