Hello, I am a newbie to ruby

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

···

On Sep 21, 2005, at 7:23 AM, could ildg wrote:

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?

could ildg wrote:

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.

···

--
  Phlip
  greencheese.org <-- NOT a blog!!!

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

···

On Sep 21, 2005, at 6:23 AM, could ildg wrote:

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?

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 :

···

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?

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:could.net@gmail.com]
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.....

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.

···

On 9/21/05, Gavin Kistner <gavin@refinery.com> wrote:

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

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

Rob

···

On 9/21/05, Josh Charles <josh.charles@gmail.com> wrote:

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.

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

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

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

···

On Sep 21, 2005, at 9:23 AM, Josh Charles wrote:

On 9/21/05, Gavin Kistner <gavin@refinery.com> wrote:

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:

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

Rob

···

On 9/21/05, Rob Rypka <rascal1182@gmail.com> wrote:

On 9/21/05, Josh Charles <josh.charles@gmail.com> wrote:
>
> 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

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)

···

On 9/21/05, Rob Rypka <rascal1182@gmail.com> wrote:

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