📜  Ruby和C语言之间的异同

📅  最后修改于: 2021-05-26 01:42:31             🧑  作者: Mango

Ruby和C之间的相似之处

C和Ruby之间有很多相似之处,其中一些是:

像C一样,在Ruby中…

  • 程序员可以根据自己的意愿进行程序编程。但是,在幕后,它将仍然是面向对象的。
  • 两种语言具有相同的运算符,例如,复合赋值和按位运算运算符。但是Ruby没有++或–像C一样。
  • 他们两个都拥有__FILE__和__LINE__。
  • 没有特殊的const关键字,但仍然可以有常量。
  • 在C和Ruby中,字符串都用双引号引起,即“”。
  • 它们都包含可变字符串。
  • 使用ri命令,可以像在手册页中一样在终端中阅读大多数文档。
  • 两种类型的命令行调试器均可用。

    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基础课程》。