# What are the differences between c++ and Ruby?

**URL:** <https://www.ruby-forum.com/t/what-are-the-differences-between-c-and-ruby/121934>\
**Category:** Ruby\
**Created:** [December 3, 2007, 2:40am UTC](https://www.ruby-forum.com/t/what-are-the-differences-between-c-and-ruby/121934 "2007-12-03T02:40:50Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![duddillaSs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c89c15/32.png) [@duddillaSs](https://www.ruby-forum.com/u/duddillaSs)\
**Post date:** [December 3, 2007, 2:40am UTC](https://www.ruby-forum.com/t/what-are-the-differences-between-c-and-ruby/121934/1 "2007-12-03T02:40:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![duddillaSs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c89c15/32.png) [@duddillaSs](https://www.ruby-forum.com/u/duddillaSs)\
**Post date:** [December 3, 2007, 1:16pm UTC](https://www.ruby-forum.com/t/what-are-the-differences-between-c-and-ruby/121934/2 "2007-12-03T13:16:33Z")

</div>

On Dec 2, 2007 7:40 PM, duddilla’s [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![duddillaSs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c89c15/32.png) [@duddillaSs](https://www.ruby-forum.com/u/duddillaSs)\
**Post date:** [December 3, 2007, 1:39pm UTC](https://www.ruby-forum.com/t/what-are-the-differences-between-c-and-ruby/121934/3 "2007-12-03T13:39:31Z")

</div>

On Dec 3, 2007 6:15 AM, Todd B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![duddillaSs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c89c15/32.png) [@duddillaSs](https://www.ruby-forum.com/u/duddillaSs)\
**Post date:** [December 3, 2007, 3:36pm UTC](https://www.ruby-forum.com/t/what-are-the-differences-between-c-and-ruby/121934/4 "2007-12-03T15:36:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![duddillaSs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c89c15/32.png) [@duddillaSs](https://www.ruby-forum.com/u/duddillaSs)\
**Post date:** [December 3, 2007, 3:30pm UTC](https://www.ruby-forum.com/t/what-are-the-differences-between-c-and-ruby/121934/5 "2007-12-03T15:30:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![duddillaSs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c89c15/32.png) [@duddillaSs](https://www.ruby-forum.com/u/duddillaSs)\
**Post date:** [December 3, 2007, 6:00pm UTC](https://www.ruby-forum.com/t/what-are-the-differences-between-c-and-ruby/121934/6 "2007-12-03T18:00:08Z")

</div>

Robert K. 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

---

<div class="post-metadata">

**Author:** ![duddillaSs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c89c15/32.png) [@duddillaSs](https://www.ruby-forum.com/u/duddillaSs)\
**Post date:** [December 3, 2007, 4:45pm UTC](https://www.ruby-forum.com/t/what-are-the-differences-between-c-and-ruby/121934/7 "2007-12-03T16:45:24Z")

</div>

2007/12/3, Ilan B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 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

---

<div class="post-metadata">

**Author:** ![duddillaSs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c89c15/32.png) [@duddillaSs](https://www.ruby-forum.com/u/duddillaSs)\
**Post date:** [December 4, 2007, 5:38am UTC](https://www.ruby-forum.com/t/what-are-the-differences-between-c-and-ruby/121934/8 "2007-12-04T05:38:53Z")

</div>

On Mon, 3 Dec 2007, Todd B. wrote:

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

---

<div class="post-metadata">

**Author:** ![duddillaSs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c89c15/32.png) [@duddillaSs](https://www.ruby-forum.com/u/duddillaSs)\
**Post date:** [December 5, 2007, 8:39pm UTC](https://www.ruby-forum.com/t/what-are-the-differences-between-c-and-ruby/121934/9 "2007-12-05T20:39:01Z")

</div>

All people have the same basic physical traits, therefore there are no  
important differences between Sarah Jessica Parkman and Gisele BÃ¼ndchen.

---

<div class="post-metadata">

**Author:** ![duddillaSs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c89c15/32.png) [@duddillaSs](https://www.ruby-forum.com/u/duddillaSs)\
**Post date:** [December 4, 2007, 9:02am UTC](https://www.ruby-forum.com/t/what-are-the-differences-between-c-and-ruby/121934/10 "2007-12-04T09:02:59Z")

</div>

On Dec 2, 7:38 pm, “duddilla’s” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![duddillaSs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c89c15/32.png) [@duddillaSs](https://www.ruby-forum.com/u/duddillaSs)\
**Post date:** [December 6, 2007, 12:15am UTC](https://www.ruby-forum.com/t/what-are-the-differences-between-c-and-ruby/121934/11 "2007-12-06T00:15:27Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![duddillaSs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c89c15/32.png) [@duddillaSs](https://www.ruby-forum.com/u/duddillaSs)\
**Post date:** [December 6, 2007, 12:28pm UTC](https://www.ruby-forum.com/t/what-are-the-differences-between-c-and-ruby/121934/12 "2007-12-06T12:28:19Z")

</div>

2007/12/6, Vasil V. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 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

---

<div class="post-metadata">

**Author:** ![duddillaSs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c89c15/32.png) [@duddillaSs](https://www.ruby-forum.com/u/duddillaSs)\
**Post date:** [December 6, 2007, 3:36pm UTC](https://www.ruby-forum.com/t/what-are-the-differences-between-c-and-ruby/121934/13 "2007-12-06T15:36:07Z")

</div>

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
