Hello, I am a newbie to ruby.

Discussion in 'Ruby' started by could ildg, Sep 21, 2005.

  1. could ildg

    could ildg Guest

    ------=_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--
     
    could ildg, Sep 21, 2005
    #1
    1. Advertisements

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

    Welcome to Ruby!

    James Edward Gray II
     
    James Edward Gray II, Sep 21, 2005
    #2
    1. Advertisements

  3. could ildg

    Phlip Guest

    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, Sep 21, 2005
    #3
  4. 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. :)
     
    Gavin Kistner, Sep 21, 2005
    #4
  5. could ildg

    Jurgen Stroo Guest

    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 :
     
    Jurgen Stroo, Sep 21, 2005
    #5
  6. could ildg

    Josh Charles Guest

    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.
     
    Josh Charles, Sep 21, 2005
    #6
  7. could ildg

    Rob Rypka Guest

    ------=_Part_803_31746103.1127312815037
    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

    ------=_Part_803_31746103.1127312815037--
     
    Rob Rypka, Sep 21, 2005
    #7
  8. 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. ;)
    See the standard "observer" library.

    James Edward Gray II
     
    James Edward Gray II, Sep 21, 2005
    #8
  9. could ildg

    Rob Rypka Guest

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

    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--
     
    Rob Rypka, Sep 21, 2005
    #9
  10. could ildg

    Josh Charles Guest

    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)
     
    Josh Charles, Sep 21, 2005
    #10
  11. 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:]
    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.....
     
    Warren Seltzer, Sep 21, 2005
    #11
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.