What are the differences between c++ and Ruby?

D

duddilla's

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
 
T

Todd Benson

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
 
T

Todd Benson

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
 
S

Sebastian Hungerecker

duddilla's said:
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
 
I

Ilan Berci

duddilla's said:
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
 
R

Robert Klemme

2007/12/3 said:
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
 
I

Ilan Berci

Robert said:
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
 
J

John Carter

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

kevin cline

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

Vitor Peres

QWxsIHBlb3BsZSBoYXZlIHRoZSBzYW1lIGJhc2ljIHBoeXNpY2FsIHRyYWl0cywgdGhlcmVmb3Jl
IHRoZXJlIGFyZSBubwppbXBvcnRhbnQgZGlmZmVyZW5jZXMgYmV0d2VlbiBTYXJhaCBKZXNzaWNh
IFBhcmttYW4gYW5kIEdpc2VsZSBCw7xuZGNoZW4uCgpPbiBEZWMgNCwgMjAwNyA1OjM1IEFNLCBr
ZXZpbiBjbGluZSA8a2V2aW4uY2xpbmVAZ21haWwuY29tPiB3cm90ZToKCj4gT24gRGVjIDIsIDc6
MzggcG0sICJkdWRkaWxsYSdzIiA8cmFkaGlrYS5kdWRkaS4uLkBnbWFpbC5jb20+IHdyb3RlOgo+
ID4gSGkKPiA+IEkgd291bGQgbGlrZSBpbXBsZW1lbnQgbXVsdGlwbGUgaW5oZXJpdGFuY2UgaW4g
amF2YSBieSB1c2luZyBydWJ5Cj4gPiBtaXhpbiBtZWNoYW5pc20uSXMgdGhhdCBwb3NzaWJsZT8g
QW5kIG15IG90aGVyIHF1ZXN0aW9uIGlzIHdoYXQgYXJlCj4gPiBleGFjdCBkaWZmZXJlbmNlcyBi
ZXR3ZWVuIEMrKyBhbmQgUnVieT8gYW5kIFJ1YnkgYW5kIEphdmE/Cj4KPiBBbGwgdGhyZWUgbGFu
Z3VhZ2VzIGFyZSBUdXJpbmctY29tcGxldGUsIGFuZCBjb25zZXF1ZW50bHkgdGhlcmUgYXJlIG5v
Cj4gaW1wb3J0YW50IGRpZmZlcmVuY2VzIGJldHdlZW4gQyssIFJ1YnksIGFuZCBKYXZhLgo+Cj4K
Pgo=
 
V

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

Robert Klemme

2007/12/6 said:
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
 
R

Robert Parker

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
 

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

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top