------=_NextPart_000_0006_01C5D0A0.58455250
Content-Type: text/plain;
charset="us-ascii"
Content-Transfer-Encoding: quoted-printable
I am a ruby newbie. I know C++ quite well, is there a ruby to c++ =
comparison
article.
I'm not sure that such an article exists...
Since my mind still thinks in C++, I believe I can learn faster =
if
there is a C++ to ruby mapping. Does such a resource exist, can anyone =
point
me to it.=20
There are some superficial similarities: both are 'OO' languages (but there
is a lot of difference in how people define what an OO language is). However,
where as in C++ polymorphism is acheieve by means of virtual functions on
classes, in Ruby (and other dynamic languages like Smalltalk) polymorphism
is acheived by when two classes have the same interface - they don't need
to be related by inheritance. So if I've got a collection (an Array) of
Ruby objects that all respond to the to_s (to string) method, like so:
array = [1,"two", 3.0, Array]
I can iterate through array and do the following:
array.each {|item|
puts "item is: #{item.to_s} item.class is: #{item.class}"
}
and the output will be:
item is: 1 item.class is: Fixnum
item is: two item.class is: String
item is: 3.0 item.class is: Float
item is: Array item.class is: Class
No worries about collections of pointers to a base class which have a
static type but can also point to derived class objects with a dynamic type.
So whenever I'm in an interview and the interviewer asks me to explain
polymorphism I have to gauge where the inerviewer is coming from before
I answer. 9 times out of ten they want to know about virtual functions in C++
- to the C++ programmer that is what polymorphism is all about. If I answer
by explaining a more dynamic view of polymorphism (like the Ruby view) to the
C++centric interviewer (s)he might tell me I'm wrong; if that's the case it
tells me a lot about the limitations of the interviewer's worldview.
....but I digress. Try not to learn Ruby through C++ colored glasses.
C++ is all about things being nailed down at compile time (well, almost all)
while Ruby is about things happening in a very dynamic way during runtime.
(this could include things which are unimaginable to a C++ programmmer, things
like dynamic inheritance, adding methods to classes or objects at runtime etc.)
Because of Ruby's dynamic nature there are lots of things that can be done
in a few lines of Ruby that could take hundreds of lines of C++ to
accomplish.
Of course the downside of all this dynamicity is that Ruby programs tend to
run a good deal slower than C++ programs. Most of the time Ruby's performance
is adequate, but sometimes you really need more performance. I'm currently
working on a prototype in Ruby (think of Ruby as modelling clay) and when
all of the functionality is there I'm going to have to translate it to C++
to improve performance (C++ is the marble in this sculpting analogy).
Anyway, that's a brief philosophical comparison of the two languages.
Hope it helps.
BTW: Notice in my array example above that even classes like the class Array
are objects in Ruby. This is another very large difference between the two
languages and it has lots of implications....
Phil