Hello, I am a newbie to ruby.

C

could ildg

------=_Part_183_19166653.1127305376575
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

I want learn a script language.
After days of investigation, I decide to choose ruby, as I'm familiar with
basic.
But at first I have a question.
I heard that ruby only supports single inheritance and it has no interface,
Is that true? Then what should I do if I really want multi-inheritance?

------=_Part_183_19166653.1127305376575--
 
J

James Edward Gray II

I want learn a script language.
After days of investigation, I decide to choose ruby, as I'm
familiar with
basic.
But at first I have a question.
I heard that ruby only supports single inheritance and it has no
interface,
Is that true? Then what should I do if I really want multi-
inheritance?

We use "Mix-ins" for that. You'll learn the details as you go.

Welcome to Ruby!

James Edward Gray II
 
P

Phlip

could said:
I heard that ruby only supports single inheritance and it has no
interface, Is that true? Then what should I do if I really want
multi-inheritance?

Ruby uses dynamically typed polymorphism, and everything is an object,
including classes. You can do

Your question matters for a statically typed language, where classes are not
objects. Such languages need extra stuff to support their behavior.

Java: public void foo(Bar bar) {}

foo is a method that takes a bar of type Bar. We need interfaces to allow
many things to inherit from Bar and change bar's behavior.

Ruby: def foo(bar) ; end

We are not required to declare the type of bar. It can be anything that
provides methods for the messages foo will send to it.

So you can "inherit interface" simply by making two classes look the same.

Java does not support multiple inheritance of implementation. Ruby does not
either, but in theory you should need it ;-). In practice, there are several
cute ways to delegate.
 
G

Gavin Kistner

I heard that ruby only supports single inheritance and it has no
interface,
Is that true? Then what should I do if I really want multi-
inheritance?

If you really want multi-inheritance, you should ask yourself "Why do
I want this?"

Then you should tell us, so that we can show you how the same goals
can be achieved, simply, in Ruby. :)
 
J

Jurgen Stroo

Hi! I am new to Ruby too, I am reading about it the last two days.
I read about Mixins, which can be the solution for you, if multiple
inheritance is needed. Ruby itself has single-parented classes. But,
according to the book Programming Ruby, Ruby classes can include the functionality of any number of mixins, which are
partial class definitions.
You should read about it, so you can find out how this works.

Jurgen

### http://bash.org/?23601 - bash.org goodie ###
<mage> what should I give sister for unzipping?
<Kevyn> Um. Ten bucks?
<mage> no I mean like, WinZip?

Although very unlikely, it seems could ildg stated on Sep 21 that :
 
J

Josh Charles

If you really want multi-inheritance, you should ask yourself "Why do
I want this?"

Then you should tell us, so that we can show you how the same goals
can be achieved, simply, in Ruby. :)

One place multiple inheritance / interfaces really shines is with
adapters and the observer pattern. I've not tried either of these
with ruby yet, however, so I'm unfamiliar with how these problems
would be solved with ruby.

Adapter: you have a common interface for accessing the database, so
that if the database changes, you just change the adapter, and nothing
else changes.

Observer: the ability to update several objects when one main object
changes state. The observers are usually registered with the main
object. These observers could possibly be several different types of
data structures, but the main object requires a certain interface in
order to insure that the observer is updated.
 
R

Rob Rypka

------=_Part_803_31746103.1127312815037
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

One place multiple inheritance / interfaces really shines is with
adapters and the observer pattern. I've not tried either of these
with ruby yet, however, so I'm unfamiliar with how these problems
would be solved with ruby.

The "Ruby Way" is to use Procs. This explains it better that I would:

http://www.rubygarden.org/ruby?ObserverPattern

Rob

------=_Part_803_31746103.1127312815037--
 
J

James Edward Gray II

One place multiple inheritance / interfaces really shines is with
adapters and the observer pattern. I've not tried either of these
with ruby yet, however, so I'm unfamiliar with how these problems
would be solved with ruby.

Adapter: you have a common interface for accessing the database, so
that if the database changes, you just change the adapter, and nothing
else changes.

You're still thinking like a Java programmer here. Us Rubyists are
big believers in Duck Typing.
The saying goes, "If it walks like a duck and talks like a duck, it's
a duck." In other words, if it supports the method calls we want,
we're happy. We generally don't even check for the methods. If it's
not there, an exception will be thrown.

The hint earlier given to post some code and let us Rubyify it was a
good suggestion. You just need to get into the Ruby mind set. Free
your mind. ;)
Observer: the ability to update several objects when one main object
changes state. The observers are usually registered with the main
object. These observers could possibly be several different types of
data structures, but the main object requires a certain interface in
order to insure that the observer is updated.

See the standard "observer" library.

James Edward Gray II
 
R

Rob Rypka

------=_Part_931_26763766.1127313594089
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

The "Ruby Way" is to use Procs. This explains it better that I would:

http://www.rubygarden.org/ruby?ObserverPattern

Rob

That response was incomplete. The above is for the Observer pattern.

Also, s/that/than/. I woke up way too early this morning, and my
communication skills are suffering for it.

For completeness, here's a link for the adaptor pattern:

http://www.rubygarden.org/ruby?AdaptorPattern


If you're interested in other design patterns in Ruby, find them here:

http://www.rubygarden.org/ruby?ExampleDesignPatternsInRuby

Rob

------=_Part_931_26763766.1127313594089--
 
J

Josh Charles

That response was incomplete. The above is for the Observer pattern.

Also, s/that/than/. I woke up way too early this morning, and my
communication skills are suffering for it.

For completeness, here's a link for the adaptor pattern:

http://www.rubygarden.org/ruby?AdaptorPattern


If you're interested in other design patterns in Ruby, find them here:

http://www.rubygarden.org/ruby?ExampleDesignPatternsInRuby

Rob

Those links were helpful, though I had to reread the adapter code
again before I understood it. It was partly because it seemed like
the code was repeating itself, but then the lightbulb went on, and I
realized that this is quicker than doing it in C# (my 'previous'
language)
 
W

Warren Seltzer

The answers to your questions depend on what you are planning to do with the script
language.

Warren Seltzer


-----Original Message-----
From: could ildg [mailto:[email protected]]
Sent: Wednesday, September 21, 2005 3:23 PM
To: ruby-talk ML
Subject: Hello, I am a newbie to ruby.


I want learn a script language.....
 

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
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top