Ruby 和 C 之间的相似之处
C 和 Ruby 之间有很多相似之处,其中一些是:
像 C 一样,在 Ruby 中也……
Ruby 和 C 之间的差异
Ruby | C |
---|---|
In Ruby, there is no need to compile the code, it can be run directly. | In C, compilation of code is necessary because it cannot be run directly. |
It’s require ‘foo’ instead of #include or #include “foo”. | Nothing like that is required in C. |
There are no variable declarations in Ruby. | Variable declaration is necessary in C. |
In Ruby, there’s no macros or pre-processor, no casts, no pointers, no typedefs, sizeof, nor enums available. | Whereas, they are present in C. |
Arguments to methods (i.e. functions) are passed by value, where the values are always object references. | In C functions are passed by value as well as passed by reference. |
Parentheses for method (i.e. function) calls are often optional. | This is not optional in C. |
There is no char—they are just 1-letter strings. | Char is used in C for one character. |
Array literals go in brackets instead of braces in Ruby. | Array literals go in braces in C. |
You cannot drop down to assembly. | In C you cannot drop down to assembly. |
In Ruby, objects are strongly typed. | In C, objects are not strongly typed. |
You go without parentheses for if and while condition expressions. | Parentheses are needed in C with if and while expressions. |
Strings don’t end with a null byte in Ruby. | While strings ends with a null byte in C. |
If you add two arrays, you get back a new and bigger array (of course, allocated on the heap) instead of doing pointer arithmetic. | There is a need of pointer arithmetic in C. |
In Ruby, arrays just automatically get bigger when you stuff more elements into them. | In C, automatically array cannot gets bigger. |
All variables live on the heap. Further, you don’t need to free them yourself—the garbage collector takes care of that. | In C, we need to free them ourself because garbage collector is not present in C. |
You don’t usually use braces—just end multi-line constructs (like whileloops) with an end keyword. | Braces are required because ignoring braces will cause syntax error. |
All the functions and classes are defined in the main source code files as there are no header files present in ruby. | Header files are present in C. |
There’s no semicolons ending lines. | There is ending lines. |
There’s no #define in ruby. Just use constants. | #define is there in C. |
The do keyword is for so-called “blocks”. There’s no “do statement” like in C. | Do statement is used in C with while to make a loop like do-while. |
想要从精选的视频和练习题中学习,请查看C 基础到高级C 基础课程。