What are the differences between c++ and Ruby?

Discussion in 'Ruby' started by duddilla's, Dec 3, 2007.

  1. duddilla's

    duddilla's Guest

    Hi
    I would like implement multiple inheritance in java by using ruby
    mixin mechanism.Is that possible? And my other question is what are
    exact differences between C++ and Ruby? and Ruby and Java?

    Thanks
    duddilla's, Dec 3, 2007
    #1
    1. Advertising

  2. duddilla's

    Todd Benson Guest

    On Dec 2, 2007 7:40 PM, duddilla's <> wrote:
    > Hi
    > I would like implement multiple inheritance in java by using ruby
    > mixin mechanism.Is that possible? And my other question is what are
    > exact differences between C++ and Ruby? and Ruby and Java?
    >
    > Thanks


    You do understand, of course, that to answer these questions, one
    might have to write an entire book. By asking these, you come
    dangerously close to a RTFM response.

    Java cannot behave exactly like Ruby when it comes to module/mixin
    behavior if that's what you're asking.

    Todd
    Todd Benson, Dec 3, 2007
    #2
    1. Advertising

  3. duddilla's

    Todd Benson Guest

    On Dec 3, 2007 6:15 AM, Todd Benson <> wrote:
    > Java cannot behave exactly like Ruby when it comes to module/mixin
    > behavior if that's what you're asking.


    I should qualify ... I'm not sure this last statement of mine is
    entirely true. It has been a while since I've used Java for
    productive use. I'd like to dive into JRuby, but just haven't had the
    time recently. Ah, well...

    Todd
    Todd Benson, Dec 3, 2007
    #3
  4. duddilla's wrote:
    > And my other question is what are
    > exact differences between C++ and Ruby? and Ruby and Java?


    There are a lot of differences. Here are some that come to mind right away:

    In ruby everthing is an object:
    5.class #=> Integer
    Integer.class #=> Class

    Ruby has convenient introspection methods:
    some_object.class # What's some_object's class?
    some_object.respond_to?("meth") # Does some_object have the method "meth"
    some_object.methods # Which methods does this object have
    SomeClass.instance_methods # Which methods do instances of the class have
    ...

    In ruby you can reopen classes:
    5.double #=> Error: No such method
    class Integer
    def double()
    self * 2
    end
    end
    5.double #=> 10

    Ruby has dynamic typing:
    x = 5
    x.class #=> Integer
    x = "lala"
    x.class #=> String

    Ruby has blocks and a lot of methods that make good use of them:
    [1,2,3,4].map {|x| x+3} #=> [4,5,6,7]
    [5,7,11,8].any? {|x| x>10} #=> true
    [5,7,11,8].all? {|x| x>10} #=> false
    5.times { puts "Hello world" } # Prints "Hello World" five times.

    In ruby you can easily define methods dynamically:
    class String
    (2..10).each do |i|
    define_method("print_#{i}_times") do
    i.times {puts self}
    end
    end
    end
    "hi".print_4_times # Prints "hi" 4 times

    And a lot of other things.


    HTH,
    Sebastian
    --
    Jabber:
    ICQ: 205544826
    Sebastian Hungerecker, Dec 3, 2007
    #4
  5. duddilla's

    Ilan Berci Guest

    duddilla's wrote:
    > Hi
    > I would like implement multiple inheritance in java by using ruby
    > mixin mechanism.Is that possible? And my other question is what are
    > exact differences between C++ and Ruby? and Ruby and Java?
    >
    > Thanks


    Java does not support MI nor will it ever support MI. In fact one of
    the reasons it initially gained popularity IMO was that it didn't
    support MI. Java decided to stay away from the major compiler
    complexity head ache and developers never looked back.

    MI in C++ eventually leads to diamond ambiguities which eventually leads
    to scope resolution operators spread throughout your entire code base
    and/or private inheritance. For more on this subject, please consult
    Lippmann, Meyers, Fowler

    Oh... one more thing.. mixins are not MI. Ruby doesn't support MI and I
    believe it doesn't intend to. Anyone who has dealt with diamond
    ambiguities is very glad of this fact. Each class in Ruby has one and
    only one super.

    ilan


    --
    Posted via http://www.ruby-forum.com/.
    Ilan Berci, Dec 3, 2007
    #5
  6. 2007/12/3, Ilan Berci <>:
    > MI in C++ eventually leads to diamond ambiguities which eventually leads
    > to scope resolution operators spread throughout your entire code base
    > and/or private inheritance. For more on this subject, please consult
    > Lippmann, Meyers, Fowler
    >
    > Oh... one more thing.. mixins are not MI. Ruby doesn't support MI and I
    > believe it doesn't intend to. Anyone who has dealt with diamond
    > ambiguities is very glad of this fact. Each class in Ruby has one and
    > only one super.


    Mostly agree. But: you can include any number of modules and thus
    achieve /MI of behavior/ in Ruby. The diamond issue does not come up
    for two reasons: first, there is just one scope for instance
    variables, not multiple like in C++. Second, Ruby imposes an order on
    all super classes and mixins, that's why there is only one "super".
    So while Ruby works differently than C++ and Eiffel you can justify
    the claim that it supports MI.

    Kind regards

    robert

    --
    use.inject do |as, often| as.you_can - without end
    Robert Klemme, Dec 3, 2007
    #6
  7. duddilla's

    Ilan Berci Guest

    Robert Klemme wrote:

    >
    > Mostly agree. But: you can include any number of modules and thus
    > achieve /MI of behavior/ in Ruby. The diamond issue does not come up
    > for two reasons: first, there is just one scope for instance
    > variables, not multiple like in C++. Second, Ruby imposes an order on
    > all super classes and mixins, that's why there is only one "super".
    > So while Ruby works differently than C++ and Eiffel you can justify
    > the claim that it supports MI.
    >
    > Kind regards
    >
    > robert


    I stand corrected, thanks for the clarification Robert

    ilan
    --
    Posted via http://www.ruby-forum.com/.
    Ilan Berci, Dec 3, 2007
    #7
  8. duddilla's

    John Carter Guest

    On Mon, 3 Dec 2007, Todd Benson wrote:

    > On Dec 2, 2007 7:40 PM, duddilla's <> wrote:
    >> Hi
    >> I would like implement multiple inheritance in java by using ruby
    >> mixin mechanism.Is that possible? And my other question is what are
    >> exact differences between C++ and Ruby? and Ruby and Java?
    >>
    >> Thanks

    >
    > You do understand, of course, that to answer these questions, one
    > might have to write an entire book. By asking these, you come
    > dangerously close to a RTFM response.


    Don't be so unhelpful... it's obvious that Ruby is like a writing desk
    and C++ is like a Raven.

    (Before you go ballistic... Read Alice in Wonderland and realize I'm
    pulling legs left right and center! :))

    C++ statically type, compiled. Ruby dynamically (duck typed) interpreted.

    C++ designed as an incremental improvement on C to improve code reuse
    whilst adding the minimum number of new keywords. (If you think about
    it, that's a really really lousy objective function.)

    Ruby designed as a best of all worlds (perl/smalltalk/....) new language.

    Java statically typed compile to interpreted byte code.

    C++ and Java are not "objects all the way down" Ruby is.

    C++ has multiple inheritance, Java has single inheritance but "extends" stateless interfaces.

    You can duplicate Javarish behaviour in C++ by inheritaing from
    stateless abstract class where all methods are "pure virtual".

    You can emulate Rubyish mixin behaviour in C++ by multiple inheritance
    from stateless classes.

    There is no easy root to emulate mixins in java.
    John Carter, Dec 4, 2007
    #8
  9. duddilla's

    kevin cline Guest

    On Dec 2, 7:38 pm, "duddilla's" <> wrote:
    > Hi
    > I would like implement multiple inheritance in java by using ruby
    > mixin mechanism.Is that possible? And my other question is what are
    > exact differences between C++ and Ruby? and Ruby and Java?


    All three languages are Turing-complete, and consequently there are no
    important differences between C+, Ruby, and Java.
    kevin cline, Dec 4, 2007
    #9
  10. duddilla's

    Vitor Peres Guest

    QWxsIHBlb3BsZSBoYXZlIHRoZSBzYW1lIGJhc2ljIHBoeXNpY2FsIHRyYWl0cywgdGhlcmVmb3Jl
    IHRoZXJlIGFyZSBubwppbXBvcnRhbnQgZGlmZmVyZW5jZXMgYmV0d2VlbiBTYXJhaCBKZXNzaWNh
    IFBhcmttYW4gYW5kIEdpc2VsZSBCw7xuZGNoZW4uCgpPbiBEZWMgNCwgMjAwNyA1OjM1IEFNLCBr
    ZXZpbiBjbGluZSA8a2V2aW4uY2xpbmVAZ21haWwuY29tPiB3cm90ZToKCj4gT24gRGVjIDIsIDc6
    MzggcG0sICJkdWRkaWxsYSdzIiA8cmFkaGlrYS5kdWRkaS4uLkBnbWFpbC5jb20+IHdyb3RlOgo+
    ID4gSGkKPiA+IEkgd291bGQgbGlrZSBpbXBsZW1lbnQgbXVsdGlwbGUgaW5oZXJpdGFuY2UgaW4g
    amF2YSBieSB1c2luZyBydWJ5Cj4gPiBtaXhpbiBtZWNoYW5pc20uSXMgdGhhdCBwb3NzaWJsZT8g
    QW5kIG15IG90aGVyIHF1ZXN0aW9uIGlzIHdoYXQgYXJlCj4gPiBleGFjdCBkaWZmZXJlbmNlcyBi
    ZXR3ZWVuIEMrKyBhbmQgUnVieT8gYW5kIFJ1YnkgYW5kIEphdmE/Cj4KPiBBbGwgdGhyZWUgbGFu
    Z3VhZ2VzIGFyZSBUdXJpbmctY29tcGxldGUsIGFuZCBjb25zZXF1ZW50bHkgdGhlcmUgYXJlIG5v
    Cj4gaW1wb3J0YW50IGRpZmZlcmVuY2VzIGJldHdlZW4gQyssIFJ1YnksIGFuZCBKYXZhLgo+Cj4K
    Pgo=
    Vitor Peres, Dec 5, 2007
    #10
  11. Why would you want to implement multiple inheritance in Java???
    if Java should have had multiple inheritance the designers wouldn't
    have bothered to add interfaces to the language. By using interfaces
    you gain the benefits of multiple inheritance without added
    complications. And the differences between Ruby and C++ don't even
    come near the differernces between C++ and Java. C++ and Ruby are from
    different planets.
    Vasil Vangelovski, Dec 5, 2007
    #11
  12. 2007/12/6, Vasil Vangelovski <>:
    > Why would you want to implement multiple inheritance in Java???
    > if Java should have had multiple inheritance the designers wouldn't
    > have bothered to add interfaces to the language. By using interfaces
    > you gain the benefits of multiple inheritance without added
    > complications.


    Actually you do not get *all* the benefits of MI, especially you are
    lacking implementation inheritance. If you want a thorough analysis I
    recommend reading Bertrand Meyer's "Object-oriented software
    construction". Eiffel has sophisticated mechanisms to allow for MI
    without nasty effects and the book provides *a lot* insight into OOx.

    > And the differences between Ruby and C++ don't even
    > come near the differernces between C++ and Java. C++ and Ruby are from
    > different planets.


    :)

    Cheers

    robert

    --
    use.inject do |as, often| as.you_can - without end
    Robert Klemme, Dec 6, 2007
    #12
  13. Hi Guys,
    I'm in here late but not matter,

    They are both object oriented languages but with important differences:
    1. In C++ there must be just 2 or 3 objects.
    2. If 2 they must be named foo and bar. Foo may inherit from bar or vice versa.
    3. If there are 3 objects , the third must be named baz and it must
    inherit from both foo and bar.

    Or so the literature about it would suggest.

    Cheers Bob
    --
    Balanced diet: Red meat, white meat, corned meat, smoked meat and sausages.
    Robert Parker, Dec 6, 2007
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Thomas Fritsch
    Replies:
    2
    Views:
    988
    Andrey Kuznetsov
    Jul 27, 2006
  2. Home_Job_opportunity
    Replies:
    0
    Views:
    482
    Home_Job_opportunity
    Jan 8, 2009
  3. Home_Job_opportunity
    Replies:
    0
    Views:
    568
    Home_Job_opportunity
    Jan 14, 2009
  4. Kevin Watkins
    Replies:
    2
    Views:
    163
    Kevin Watkins
    Apr 8, 2004
  5. Max Cantor
    Replies:
    16
    Views:
    422
    Steven Parkes
    May 10, 2008
Loading...

Share This Page