Similarities and Differences between Ruby and C++

There are many similarities between C++ and Ruby, some of them are:

Just like C++, in Ruby…

  • As in C++, public, private, and protected works similarly in Ruby also .
  • Inheritance syntax is still only one character, but it’s < instead of : in Ruby.
  • The way ‘namespace’ is used in C++, in the similar way we may put our code into “modules” in ruby.
  • There are many similar operators found in Ruby as found in C++.
  • Though the keyword names have been changed to protect the innocent, exceptions work in a similar manner.
  • Difference between Ruby and C++.

    Ruby C++
    In Ruby, every variable is just an automatically dereferenced name for some object which means that there is no explicit references in Ruby. Unlike Ruby there is explicit references in C++.
    Objects are strongly but dynamically typed in Ruby. Objects are not strongly typed as that in Ruby.
    The “constructor” is called initialize instead of the class name. This is not the case in C++.
    Array and Hash are only two container types. There are many container types in C++.

    There’s no need of C++ templates. No casting either required. C++ templates are necessary here. Casting is there.
    It’s self instead of this. It is this instead of self.
    Iteration is done a bit differently. In Ruby, you don’t use a separate iterator object. Instead you use an iterator method of the container object that takes a block of code to which it passes successive elements. Vectors are required and used in C++ which makes the coding easy.
    lib, a unit testing, comes standard with Ruby. This is not available with C++.

    There’s no type conversions in Ruby. Type conversion is necessary in C++.
    There are some enforced case-conventions. No such case conventions are present in C++ which makes it easy.
    You can re-open a class anytime and also you can add more methods in Ruby. We cannot do this in C++.
    Some methods end in a ’?’ or a ’!’ in Ruby. It’s actually part of the method name. No such symbols are required at the end of methods in C++.
    All methods are always virtual in Ruby. Methods are not virtual in C++.
    Multithreading is built-in, but as of Ruby 1.8 they are “green threads” as opposed to native threads. Multithreading is not built-in in case of C++.
    Parentheses used for calling of method are usually optional in Ruby. Parentheses are required in C++ and it is necessary.
    You don’t directly access member variables—all access to public member variables is via methods. Member variables in C++ can be access directly.


    My Personal Notes arrow_drop_up