What are the differences between c++ and Ruby?

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

On Dec 2, 2007 7:40 PM, duddilla’s [email protected] 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

On Dec 3, 2007 6:15 AM, Todd B. [email protected] 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

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

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

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

2007/12/3, Ilan B. [email protected]:

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

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.

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

On Dec 2, 7:38 pm, “duddilla’s” [email protected] 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.

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.

2007/12/6, Vasil V. [email protected]:

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.

:slight_smile:

Cheers

robert

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs