New to this language

Hey,

OH ...I get it now !!! yipeee....this makes so much more sense!

Cheers,
Vidhi

···

-----Original Message-----
From: Forrest Chang [mailto:fkchang2000@yahoo.com]
Sent: Friday, February 11, 2005 10:00 AM
To: ruby-talk ML
Subject: Re: new to this language

Hi Vidhi:

  You mentioned being only somewhat familiar with
C++/Java, but what Joao mentioned, is basically the
functional equivalent of a main() in a Java class.
This allows you to run the file by itself, i.e. ruby
file.rb, and allow you to easily reuse the contents in
another file, i.e.

(hello.rb)
def hello
  puts "hello"
end

if $0 == __FILE__
  hello
end

so you run it and you get

ruby hello.rb

hello

Compilation finished at Fri Feb 11 09:58:52

so now you want to reuse the contents of hello.rb
(beatles.rb)
require "hello"

def song_chorus
  hello
  puts "goodbye"
end
if $0 == __FILE__
  song_chorus
end

you run this file and get a

hello
goodbye

  Hope this helps.

Forrest
--- Joao Pedrosa <joaopedrosa@gmail.com> wrote:

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

Hi,

Hey,

OH ...I get it now !!! yipeee....this makes so much more sense!

That's cool, nonetheless. :slight_smile: Fun. :slight_smile:

Cheers,
Vidhi

From: Forrest Chang [mailto:fkchang2000@yahoo.com]
Sent: Friday, February 11, 2005 10:00 AM
To: ruby-talk ML
Subject: Re: new to this language

Hi Vidhi:

  You mentioned being only somewhat familiar with
C++/Java, but what Joao mentioned, is basically the
functional equivalent of a main() in a Java class.
This allows you to run the file by itself, i.e. ruby
file.rb, and allow you to easily reuse the contents in
another file, i.e.

(hello.rb)
def hello
  puts "hello"
end

if $0 == __FILE__
  hello
end

so you run it and you get
> ruby hello.rb
hello

Compilation finished at Fri Feb 11 09:58:52

so now you want to reuse the contents of hello.rb
(beatles.rb)
require "hello"

def song_chorus
  hello
  puts "goodbye"
end
if $0 == __FILE__
  song_chorus
end

you run this file and get a

hello
goodbye

  Hope this helps.

Forrest

> 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
>
>

Regards,
Joao

···

On Sat, 12 Feb 2005 03:10:50 +0900, Ghelani, Vidhi <vidhi.ghelani@intel.com> wrote:

-----Original Message-----
--- Joao Pedrosa <joaopedrosa@gmail.com> wrote: