new to this language

G

Ghelani, Vidhi

------_=_NextPart_001_01C5105B.49D44EFF
Content-Type: text/plain;
charset="us-ascii"
Content-Transfer-Encoding: quoted-printable

Hi,

=20

I am completely new to this language. I am trying to learn it on my own
by reading the book available online. I wanted to type some code and
test it while I was learning.=20

I was wondering if someone could answer the following questions that
would help me get started on writing some small amount of code testing
it myself.

=20

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 ?

=20

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

=20

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?

=20

These questions may seem really stupid, but I have no idea about this
language. I have a little knowledge of C and Java, but not ruby.

=20

Any help is appreciated,=20

=20

Thanks in advance,

Vidhi.

=20

=20


------_=_NextPart_001_01C5105B.49D44EFF--
 
J

Jim Menard

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
 
C

Caio Tiago Oliveira

Hi,

Ghelani, Vidhi, 11/2/2005 14:01:
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 ?

rb


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


Ruby is not compiled, it's interpreted. To run a file you can use 'ruby
foo.rb', when 'foo' is the name of your file.

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 either a makefile.
 
J

Joao Pedrosa

Hi,
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
 
B

Brian Schröder

Vidhi,

Welcome to Ruby.


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


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


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.

one thing I want to add, is that you need not create a seperate file per class. You can as well group classes by functionality and put multiple classes in one file. (I think using one file per class is a java idiom).


first.rb
class Greet
def initialize(name)
@name = name
end
end

class GreetEnglish < Greet
def greetme
puts "Hello #{name}"
end
end

class GreetGerman < Greet
def greetme
puts "Hallo #{name}"
end
end

class GreetSpanish < Greet
def greetme
puts "Hola #{name}"
end
end

greeters = [GreetEnglish, GreetGerman, GreetSpanish]
greeter = greeters[rand(greeters.length)].new
greeter.greet

is perfectly reasonable and allowed. (Though maybe not two good design, and you can shorten this alot when you have learned about dynamic programming).

And additionally, if you want to play with the language use irb, the ruby interactive shell.

good luck and enjoy ruby,

Brian
 
J

James Edward Gray II

Hi,
Hello.

I am completely new to this language. I am trying to learn it on my own
by reading the book available online. I wanted to type some code and
test it while I was learning.

Welcome to Ruby.
I was wondering if someone could answer the following questions that
would help me get started on writing some small amount of code testing
it myself.

I'll sure try...
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 ?

Ruby reads plain text files. It you make a test file that's full of
Ruby code, you're good to go.

The traditional extension for these files is .rb, but I don't think
Ruby really cares. Extensions are mainly used to support Windows and
give hints to various editors/IDEs.

Unix (and Mac OS X) cares more about the "shebang" line. The first
line of a Unix script is often a "sharp" (#), followed by a "bang" (!),
followed by the path to Ruby. Unix uses this when the file is executed
to determine what to run it with. Here's the shebang line for my Mac
OS X install:

#!/usr/local/bin/ruby

A lot of people prefer:

#!/usr/bin/env ruby

That shebang can often find the right Ruby to run on differing systems.

Again, Ruby itself doesn't much care, but this is support Unix.

So, we can combine those practices to create pretty standard Ruby
source that should work in most places. Here's the famous "Hello
World" example, which you could save in something like "hello.rb":

#!/usr/bin/env ruby

puts "Hello world!"

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

Ruby does away with that icky "compile" step. Just run your code:

% ruby hello.rb

Better, run your code with warnings turned on, so Ruby can help you
find bugs:

% ruby -w hello.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?

In Ruby, "main" is the code that appears outside methods. Just put it
in your file and it will be run.
These questions may seem really stupid, but I have no idea about this
language. I have a little knowledge of C and Java, but not ruby.

Again, welcome and just shout if we can be of more help.

James Edward Gray II
 
F

Forrest Chang

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

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
474,167
Messages
2,570,910
Members
47,453
Latest member
MadelinePh

Latest Threads

Top