[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

new to this language

Ghelani, Vidhi

2/11/2005 5:02:00 PM

Hi,



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.

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.



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 ?



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



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?



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.



Any help is appreciated,



Thanks in advance,

Vidhi.





9 Answers

Jim Menard

2/11/2005 5:15:00 PM

0

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



Caio Tiago Oliveira

2/11/2005 5:28:00 PM

0

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.



Joao Pedrosa

2/11/2005 5:36:00 PM

0

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


Brian Schröder

2/11/2005 5:37:00 PM

0

On Sat, 12 Feb 2005 02:15:18 +0900
Jim Menard <jimm@io.com> wrote:

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

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


James Gray

2/11/2005 5:45:00 PM

0

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

> 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



Forrest Chang

2/11/2005 6:00:00 PM

0

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



Bill Guindon

2/11/2005 7:26:00 PM

0

On Sat, 12 Feb 2005 02:01:31 +0900, Ghelani, Vidhi
<vidhi.ghelani@intel.com> wrote:
> Hi,
>
> I am completely new to this language. I am trying to learn it on my own
> by reading the book available online.

If you've not seen these yet, you may want to check them out:

http://www.zenspider.com/Languages/Ruby/Qui...
http://www.rubygarden.org/rub...
http://ruby-forum.org/bb...

more here:
http://www.rubygarden.org/ruby?Ru...

--
Bill Guindon (aka aGorilla)


Chris Ruzin

2/11/2005 10:55:00 PM

0

Bill Guindon wrote:

> On Sat, 12 Feb 2005 02:01:31 +0900, Ghelani, Vidhi
> <vidhi.ghelani@intel.com> wrote:
>> Hi,
>>
>> I am completely new to this language. I am trying to learn it on my own
>> by reading the book available online.
>
> If you've not seen these yet, you may want to check them out:
>
> http://www.zenspider.com/Languages/Ruby/Qui...
> http://www.rubygarden.org/rub...
> http://ruby-forum.org/bb...
>
> more here:
> http://www.rubygarden.org/ruby?Ru...
>

Thanks for those links. I'm also new to the language, so they were very
helpful. Especially the Zen Spider one.

-Chris

Ezra Zygmuntowicz

2/11/2005 11:19:00 PM

0

Here's a great link that is not published much but has a ton of code
examples that I found very useful:
http://pleac.sourceforge.net/pleac_ruby/...
-Ezra
On Feb 11, 2005, at 2:55 PM, Chris Ruzin wrote:

> Bill Guindon wrote:
>
>> On Sat, 12 Feb 2005 02:01:31 +0900, Ghelani, Vidhi
>> <vidhi.ghelani@intel.com> wrote:
>>> Hi,
>>>
>>> I am completely new to this language. I am trying to learn it on my
>>> own
>>> by reading the book available online.
>>
>> If you've not seen these yet, you may want to check them out:
>>
>> http://www.zenspider.com/Languages/Ruby/Qui...
>> http://www.rubygarden.org/rub...
>> http://ruby-forum.org/bb...
>>
>> more here:
>> http://www.rubygarden.org/ruby?Ru...
>>
>
> Thanks for those links. I'm also new to the language, so they were very
> helpful. Especially the Zen Spider one.
>
> -Chris
>
>
-Ezra Zygmuntowicz
Yakima Herald-Republic
WebMaster
509-577-7732
ezra@yakima-herald.com