I. Introduction overview.
1. Ruby language:
Ruby was created by Yukihiro “Matz” Matsumoto from February 24, 1993 and released the official version 0.95 (This is the first public release of Ruby published in Japan) on December 21, 1995. In Ruby 0.95, it has many familiar features in later versions of Ruby, including object-oriented design, classes with inheritance, mixins, loops, exception handling and garbage collection. After the release of Ruby 0.95 in 1995, several stable versions of Ruby were released in the following years:
- Ruby 1.0: December 25, 1996.
- Ruby 1.2: December 1998.
- Ruby 1.4: August 1999.
- Ruby 1.6: September 2000.
2. C / C ++ language:
The C programming language is an imperative language developed in the early 1970s by Dennis Ritchie for use in UNIX operating systems. Since then, the language has spread to many other operating systems and has become one of the most popular languages. C is the most effective and preferred language for writing system software, although it is also used for writing applications. In addition, C is often used as a medium of instruction in computer science although this language is not designed for beginners.
C is a relatively compact programming language that operates close to the hardware and is more similar to the Assembler language than most high-level languages. Moreover, C is sometimes referred to as “portable”, suggesting an important difference between it and low-level languages such as Assembler, which is that C code can be translated and executed in Most computers are superior to current languages, while Assembler can only run on specific computers. For this reason C is considered a middle-level language.
II. Compare similarities and differences.
Like C / C ++, in Ruby:
- Most of the operators are the same (including operators and bit operators). Therefore in Ruby there is no ++ or -.
- You can also create constants, without the keyword const. Constants must also follow the naming convention – names start with the capital letters used for constants.
- The string is enclosed in quotation marks.
- You also have the same debug command line.
- public, private, and protected also work the same.
- Exceptions also work the same, although the keyword names have been changed.
- The inheritance syntax is also just a character, but use <instead of:.
- You should put code into “modules”, like namespaces in C ++.
- You can write the procedure if you like (but it still has to be object-oriented).
Unlike C, in Ruby:
- The object is the dominant data type (and variable names need not be declared as a data type).
- There are no macros or preprocessor. No cast, no pointers (or arithmetic pointers). There are no typedefs, sizeof or enums.
- No need for header files. You simply define the function you want (usually methods) and classes in your main source code.
- No need for #define. Instead just use the constant.
- Like Ruby 1.8, code is compiled at runtime without compiling it via machine-code or byte-code.
- All variables exist on the heap. Therefore, you do not need to free it – the collector will do it.
- Method parameters (such as functions) are passed by reference, not by value.
- Use require ‘foo’ instead of #include <foo> or #include “foo”.
- You cannot drop down to assembly.
- There is no need to semicolon when the lines are over.
- No need to close parentheses when using if and while conditional expressions.
- Parentheses when calling a method (function) are usually optional.
- You do not need to use braces – just use the keyword end to end (like a while loop).
- The keyword is called “block”. There is no “due expression” as in C.
- The term “block” means there is a difference. It is a piece of code that you associate with the method and that method will execute that code when called.
- No need to declare variables. Just name it when you need it.
- When testing true and false, only false and nil are for false values. All the rest are true (including 0, 0.0, and “0”).
- No char – only one character string.
- The string does not end with an empty byte.
- Array elements are in brackets instead of curly brackets.
- The size of the array will increase as you add elements to it.
- If you add two arrays, you get a larger array of arrays (arrays of arrays) instead of pointers.
- Everything is an expression (that is, expressions like while represent a rvalue).
Ruby is a language quite similar to C / C ++, so if you want to learn ruby and are already familiar with C / C ++, you will not be surprised at the process of learning and learning this language.
You can refer to some sources below: