Couple of questions from a new user.

B

Ben Edwards

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
--
Ben Edwards - Bristol, UK
http://www.flickr.com/photos/41303704@N00/ - have a look at my pics
If you have a problem emailing me use
http://www.gurtlush.org.uk/profiles.php?uid=4
(email address this email is sent from may be defunct)
 
G

Gregory Brown

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,017
Latest member
GreenAcreCBDGummiesReview

Latest Threads

Top