📜  Ruby 和 C 语言的异同

📅  最后修改于: 2021-09-11 03:35:17             🧑  作者: Mango

Ruby 和 C 之间的相似之处

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

像 C 一样,在 Ruby 中也……

  • 如果程序员愿意,他们可以按程序进行编程。但是,在幕后,它将是面向对象的。
  • 两种语言都具有相同的运算符,例如复合赋值和按位运算运算符。但是 Ruby 没有 ++or – 像 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 基础课程