[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:47:00 PM

Hey ,

How do I get IRB?? How is this different from using SSH ?

Also, can I have different classes in a file, that are not subclasses of one another? Using your example, could I have a class in that file that was not a subclass of Greet?

Thanks,
Vidhi.


-----Original Message-----
From: Brian Schröder [mailto:ruby@brian-schroeder.de]
Sent: Friday, February 11, 2005 9:37 AM
To: ruby-talk ML
Subject: Re: new to this language

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




2 Answers

Brian Schröder

2/11/2005 6:00:00 PM

0

On Sat, 12 Feb 2005 02:46:52 +0900
"Ghelani, Vidhi" <vidhi.ghelani@intel.com> wrote:

> Hey ,
>
> How do I get IRB?? How is this different from using SSH ?

chances are good it is installed together with the ruby interpreter. Just try to enter irb at the command line. (btw. what os are you using?)

It has got nothing to do with ssh. I'll show you a typical irb session:

---
bschroed@black:~$ irb
irb(main):001:0> 1+5
=> 6
irb(main):002:0> 3.times do puts "welcome to ruby" end
welcome to ruby
welcome to ruby
welcome to ruby
=> 3
irb(main):003:0> require 'complex'
=> true
irb(main):004:0> I = Complex.new(0, 1)
=> Complex(0, 1)
irb(main):005:0> 12 + I
=> Complex(12, 1)
irb(main):006:0> (12 + I) * (2 - I)
=> Complex(25, -10)
irb(main):007:0> exit
bschroed@black:~$
---

>
> Also, can I have different classes in a file, that are not subclasses of one another? Using your example, could I have a class in that file that was not a subclass of Greet?

Yes you can. Seems it was not such a good example.

I recommend:
http://www.ruby-doc.org/docs/Progra...
http://www.ruby-doc.o...

and buy the dead tree version of the pickaxe second edition.

regards,

Brian

>
> Thanks,
> Vidhi.
>



>
> -----Original Message-----
> From: Brian Schröder [mailto:ruby@brian-schroeder.de]
> Sent: Friday, February 11, 2005 9:37 AM
> To: ruby-talk ML
> Subject: Re: new to this language
>
> 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 6:01:00 PM

0

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

> Hey ,
>
> How do I get IRB?? How is this different from using SSH ?

irb is a standard install item for Ruby. Try:

% irb

at the cammand line. This is an interactive Ruby shell. For playing
with code; trying things out. Try feeding it lines of Ruby code:

3.times { puts "Hello!" }

p [3, 5, 2, 4, 1].sort

> Also, can I have different classes in a file, that are not subclasses
> of one another? Using your example, could I have a class in that file
> that was not a subclass of Greet?

Absolutely. Mix classes and code anyway you like. Ruby won't mind.

James Edward Gray II