Ruby for C Developers
Move from C to Ruby through 48 multi-file tasks covering flow, blocks, collections, errors, classes, modules and inheritance. Build a lending desk and port two complete source programs.
C and Ruby side by side
The same program in C and in Ruby, from the lessons of this course.
Output and Comments
C starts a hosted program in main and links compiled definitions; Ruby runs top-level statements and can load neighboring files with require_relative. C printf needs a format and explicit newline, while Ruby puts appends a newline. Keep reusable methods…
C1#include <stdio.h>2 3int main(void)4{5 // Print one greeting.6 printf("%s%s\n", "Hi", ", Ada");7 return 0;8}Ruby1print "oak!"2# A real comment3puts "#seed"Conditional Values
C if is a statement, while the conditional operator selects a value. Ruby if itself is an expression whose selected branch supplies its result. Ruby also allows postfix conditional statements. Remember that zero is truthy in Ruby when adapting a C condition…
C1#include <stdio.h>2 3int main(void) {4int wet=0;const char *weather=wet?"rain":"sun";puts(weather);5return 0;6}Ruby1wet = false2icon = if wet3 "umbrella"4else5 "sun"6end7puts iconArrays and Indexing
C arrays use zero-based indexes without automatic bounds checks; reading outside their storage is undefined behavior. Ruby arrays grow dynamically, ordinary missing reads return nil, and negative indexes count from the end. Use Ruby fetch when absence should…
C1#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}Ruby1values = [4, 8]2puts values[-1]3puts(values[9].nil? ? "missing" : values[9])Mutation and Aliases
C pointer assignment aliases storage, while struct assignment copies fields and retains any copied pointer addresses. Ruby assignment shares the referenced object. Ruby reverse! mutates an array observed by every alias; reverse makes another array. C const on…
C1#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}Ruby1values = [1, 2, 3]2alias_values = values3values.reverse!4puts alias_values.join(" ")Modules as Namespaces
C organizes declarations in headers and definitions in translation units, often using name prefixes and internal linkage for isolation. Ruby modules organize constants and methods at runtime, and require_relative loads neighboring files. Loading a file does…
C1#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}Ruby1module TextTools2 def self.shout(text)3 text.upcase4 end5end6puts TextTools.shout("elm")Composing Objects
C composition can embed a value or retain a pointer to a collaborator, with different copying and lifetime implications. Ruby instance variables retain object references; assignment does not clone the collaborator or transfer exclusive ownership. Delegation…
C1#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}Ruby1class Lens2 def label; "wide"; end3end4class Camera5 def initialize(lens); @lens = lens; end6 def label; @lens.label; end7end8puts Camera.new(Lens.new).label
Syllabus
9 chapters48 lessons1 project48 challenges99 quiz questionsStart with Ruby
8 lessonsFlow and Methods
6 lessonsCollections and Blocks
6 lessonsMutation and Errors
5 lessonsObjects and State
5 lessonsComposition and Dispatch
6 lessonsObjects in Practice
6 lessonsLending Desk Project
4 lessonsPort C Programs
2 lessonsFrequently asked questions
Who is Ruby for C Developers for?
For people who already write C and want to learn Ruby. Each lesson starts from C you know and shows how Ruby does the same thing, so you skip the beginner basics.
Do I need to know Ruby already?
No. The course starts with your first Ruby program. You need C basics: variables, conditions, loops, functions and collections.
How many lessons does Ruby for C Developers have?
Ruby for C Developers contains 48 interactive lessons you can complete at your own pace.
What does Ruby for C Developers cover?
The course is organized into chapters covering: Start with Ruby, Flow and Methods, Collections and Blocks, Mutation and Errors, and more.
Is Ruby for C Developers free?
Yes. Every course on Coddy is free to start. You can complete lessons in your browser without any downloads or signup.