Menu
CoddyTech
课程

面向 C 开发者的 Ruby

通过 48 个多文件任务,从 C 过渡到 Ruby,学习流程控制、代码块、集合、错误、类、模块和继承。构建一个借阅服务台,并移植两个完整的源程序。

C 与 Ruby 对照

同一个程序分别用 C 和 Ruby 编写,取自本课程的课时。

  1. 输出与注释

    C 会在 main 中启动托管程序,并链接已编译的定义;Ruby 会运行顶层语句,也可以用 require_relative 加载相邻文件。C 的 printf 需要格式字符串和显式换行符,而 Ruby 的 puts 会自动添加换行符。请将可重复使用的方法与负责输入和输出的入口文件分开。

    C
    1#include <stdio.h>2
    3int main(void)4{5    // 输出一句问候语。6    printf("%s%s\n", "Hi", ", Ada");7    return 0;8}
    Ruby
    1print "oak!"2# 一条真正的注释3puts "#seed"
  2. 条件值

    在 C 中,if 是一条语句,而条件运算符用于选择一个值。Ruby 中的 if 本身是一个表达式,所选分支会提供它的结果。Ruby 还允许使用后置条件语句。将 C 条件改写为 Ruby 时,请记住,在 Ruby 中,0 属于真值,而不要只照搬它的写法。

    C
    1#include <stdio.h>2
    3int main(void) {4int wet=0;const char *weather=wet?"rain":"sun";puts(weather);5return 0;6}
    Ruby
    1wet = false2icon = if wet3  "umbrella"4else5  "sun"6end7puts icon
  3. 数组与索引

    C 数组使用从 0 开始的索引,且不会自动检查边界;读取其存储范围之外的内容属于未定义行为。Ruby 数组会动态增长,普通的越界读取会返回 nil,负索引则从末尾开始计数。如果缺少元素时应该触发异常,请使用 Ruby 的 fetch。C 示例会在读取前检查边界,而不是依赖缺失值结果。

    C
    1#include <stdio.h>2static int read_at(const int *values,size_t count,size_t index,int *out) {if(index>=count)return 0;*out=values[index];return 1;}3
    4int main(void) {5int values[]={4,8};size_t count=sizeof values/sizeof values[0];6printf("%d\n",values[count-1]);7int value;if(read_at(values,count,9,&value))printf("%d\n",value);else puts("missing");8return 0;9}
    Ruby
    1values = [4, 8]2puts values[-1]3puts(values[9].nil? ? "missing" : values[9])
  4. 可变性与别名

    C 指针赋值会共享存储,而结构体赋值会复制字段,并保留复制过来的任何指针地址。Ruby 赋值会共享被引用的对象。Ruby 的 reverse! 会修改数组,所有别名都能观察到这一变化;reverse 会创建另一个数组。C 中某一条访问路径上的 const,并不能普遍保证对象图彼此独立且不可变。

    C
    1#include <stdio.h>2struct Record { int n; int *nested; };3int main(void) {4    int shared = 2;5    struct Record original = {1, &shared};6    struct Record copy = original;7    copy.n = 9;8    *copy.nested = 7;9    printf("%d:%d\n", original.n, *original.nested);10    return 0;11}
    Ruby
    1values = [1, 2, 3]2alias_values = values3values.reverse!4puts alias_values.join(" ")
  5. 模块作为命名空间

    C 将声明放在头文件中,将定义放在翻译单元中,通常使用名称前缀和内部链接来实现隔离。Ruby 模块在运行时组织常量和方法,require_relative 用于加载相邻文件。加载文件不会创建与之对应的命名空间。Ruby 的 include 用于添加行为,而不是像 C 预处理器的 include 那样工作。

    C
    1#include <stdio.h>2struct Pricing { int (*subtotal)(int, int); };3static int subtotal(int price, int quantity) {4    return price * quantity;5}6static const struct Pricing *pricing_api(void) {7    static const struct Pricing api = {subtotal};8    return &api;9}10int main(void) {11    const struct Pricing *pricing = pricing_api();12    printf("%d\n", pricing->subtotal(4, 3));13    return 0;14}
    Ruby
    1module TextTools2  def self.shout(text)3    text.upcase4  end5end6puts TextTools.shout("elm")
  6. 组合对象

    在 C 中,组合可以嵌入一个值,也可以保留指向协作者的指针,两者在复制和生命周期方面的影响不同。Ruby 的实例变量保留对象引用;赋值不会克隆协作者,也不会转移其独占所有权。委托可以在不使用继承的情况下实现灵活的行为。注入一个兼容的对象,并通过预期的契约调用它的方法。

    C
    1#include <stdio.h>2struct Policy {3    int factor;4    int (*apply)(const struct Policy *self, int quantity);5};6struct Order { const struct Policy *policy; };7static int multiply(const struct Policy *self, int quantity)8{9    return self->factor * quantity;10}11static int order_total(const struct Order *self, int quantity)12{13    return self->policy->apply(self->policy, quantity);14}15int main(void)16{17    struct Policy policy = {2, multiply};18    struct Order order = {&policy};19    printf("%d\n", order_total(&order, 3));20    return 0;21}
    Ruby
    1class Lens2  def label; "wide"; end3end4class Camera5  def initialize(lens); @lens = lens; end6  def label; @lens.label; end7end8puts Camera.new(Lens.new).label

课程大纲

9 章48 节课1 个项目48 个挑战99 道测验题

更多面向 C 开发者的课程

从其他语言出发学习 Ruby

常见问题

面向 C 开发者的 Ruby 适合谁学习?

适合已经会写 C、想学 Ruby 的人。每节课都从你熟悉的 C 代码出发,展示 Ruby 如何实现同样的事情,所以你可以跳过入门基础。

需要先会 Ruby 吗?

不需要。课程从你的第一个 Ruby 程序开始。你需要掌握 C 基础:变量、条件、循环、函数和集合。

面向 C 开发者的 Ruby 有多少节课?

面向 C 开发者的 Ruby 包含 48 节互动课,你可以按自己的节奏完成。

面向 C 开发者的 Ruby 包含哪些内容?

本课程分为以下几章:从 Ruby 开始, 流程与方法, 集合与块, 可变性与错误, and more。

面向 C 开发者的 Ruby 免费吗?

是的。Coddy 上的每门课程都可以免费开始。你可以直接在浏览器中完成课程,无需下载,也无需注册。

Coddy 编程语言插图

免费开始学习 面向 C 开发者的 Ruby

开始