Hi,
I did not quite understand this :

In any file so the code in the if-block will be executed only if the
file is being run directly by the interpreter (e.g.: ruby
hello_world.rb)

So if I had a file hello_world.ruby and if I have the if block in it.
When I give the command ruby hello_world.ruby wont the entire file be
run? SO how does the if block help? 

Sorry, but I think I am understanding it wrong.

Thanks,
Vidhi







-----Original Message-----
From: Joao Pedrosa [mailto:joaopedrosa / gmail.com] 
Sent: Friday, February 11, 2005 9:36 AM
To: ruby-talk ML
Subject: Re: new to this language

Hi,

> > 3) Do you need a main and a makefile ? I am sure you would need a
main
> > to test it . If yes how do you save the main? In what format?
> 
> You don't need a main method. All Ruby code is executed as it is seen
by the
> interpreter. Some of the code above (superclass.rb and myclass.rb)
define
> classes and some of the code (another.rb) creates an instance of a
class and
> prints some output.

Else, you can use

if __FILE__ == $0 
  p 'Hello World!'
end

In any file so the code in the if-block will be executed only if the
file is being run directly by the interpreter (e.g.: ruby
hello_world.rb). It's useful so you can have a file that behaves like
a library and a program, depending on how it's loaded. You load a
library with the require (or load) command (e.g.: require 'open-uri').

I like to use such capability to test one or another thing in a file
while I'm working on it.

Welcome to Ruby.

Cheers,
Joao