[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: new to this language

Ghelani, Vidhi

2/11/2005 5:35:00 PM


Hi Jim,

Thanks a lot! That really really helped. Wow, it seems like ruby is a
pretty easy language. So I had one more question then. What if (using
the example you have given me) we wanted to run all the classes, but
they were obviously in different files. Does that mean that by giving
the command
ruby another.rb
all the files are getting executed , since the other classes are
superclasses of that class?

Also What do you mean by " All Ruby code is executed as it is seen by
the
interpreter." What is the interpreter out here? #Sorry if this is a
stupid
#question!

Thanks once again,
Vidhi.



-----Original Message-----
From: Jim Menard [mailto:jimm@io.com]
Sent: Friday, February 11, 2005 9:15 AM
To: ruby-talk ML
Subject: Re: new to this language

Vidhi,

Welcome to Ruby.

> 1) Just like in C++ you have a .h and a .cpp file , In this language
how
> would you store your file. In other words if I open emacs and want to
> write a very simple small class in what format would I save this file
?

You store all your code in .rb files. There is no separate header file.
You
declare and define a class in one place, just like in Java. (That's not
strictly true; in Ruby you can "re-open" a class and add more methods
later.
Don't worry about that yet.)

superclass.rb:

class Superclass
attr_accessor :super_instance_var
def initialize
@super_instance_var = 42
end
end

myclass.rb

require 'superclass'

class MyClass < Superclass
attr_accessor :my_instance_var
def initialize
@my_instance_var = 'hello'
end
end

another.rb

require 'myclass'

mc = MyClass.new
puts mc.my_instance_var
puts mc.super_instance_var

> 2) How do you compile your code? What is the syntax?

Ruby is an interpreted language, which means that it doesn't need to be
compiled before you run it. To run "another.rb" above, type

ruby another.rb

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

I hope this helps.

Jim
--
Jim Menard, jimm@io.com, http://www.io...





1 Answer

James Gray

2/11/2005 5:52:00 PM

0

On Feb 11, 2005, at 11:35 AM, Ghelani, Vidhi wrote:

> So I had one more question then. What if (using
> the example you have given me) we wanted to run all the classes, but
> they were obviously in different files. Does that mean that by giving
> the command
> ruby another.rb
> all the files are getting executed , since the other classes are
> superclasses of that class?

See the line in myclass.rb:

require 'superclass'

?

That loads the other file. Then in another.rb, you have:

require 'myclass'

That loads the myclass.rb file, which we've just seen loads
superclass.rb. That's how they all get pulled in.

> Also What do you mean by " All Ruby code is executed as it is seen by
> the
> interpreter." What is the interpreter out here? #Sorry if this is a
> stupid
> #question!

In Java, the program "java" is an interpreter. It reads Java bytecode
to make a program run. For Ruby, our interpreter is "ruby". "ruby"
just doesn't need the code to pass through a compiler first.

Hope that helps.

James Edward Gray II