C for Ruby Developers
Move from Ruby to C17: explicit types, pointers, bounded arrays, strings, ownership, structs and modular interfaces. Build a multi-file stock ledger and port two Ruby programs.
Ruby and C side by side
The same program in Ruby and in C, from the lessons of this course.
Printing and Comments
Ruby puts converts its argument with to_s and adds the newline for you. C printf uses a format string whose conversion specifiers must match argument types; %d prints an int. Ruby format also uses %d, but it converts or raises ArgumentError; C trusts the…
Ruby1puts "value=7"C1#include <stdio.h>2#include <stdlib.h>3#include <string.h>4#include <stddef.h>5#include <stdbool.h>6#include <limits.h>7#include <errno.h>8 9int main(void) {10printf("value=%d\n",7);11return 0;12}Booleans and Branches
Ruby treats only nil and false as falsy, so 0 and an empty String are both truthy, and emptiness is asked with empty?. In C a scalar zero is false and a nonzero scalar is true, so 0 flips meaning; a non-null pointer is true even if it points at an empty…
Ruby1n = 02s = ""3puts "#{n ? true : false} #{s ? true : false}"C1#include <stdio.h>2#include <stdlib.h>3#include <string.h>4#include <stddef.h>5#include <stdbool.h>6#include <limits.h>7#include <errno.h>8 9int main(void) {10int n=0; const char *s=""; printf("%s %s\n",n?"true":"false",s?"true":"false");11return 0;12}Arrays Need Lengths
A Ruby Array carries its length and grows with push or <<. A C array has a fixed element count, and a function array parameter is adjusted to a pointer that does not carry that count. Pass a size_t length separately and respect it; C does not check subscripts.
Ruby1a = [2, 4, 6]2puts a.lengthC1#include <stdio.h>2#include <stdlib.h>3#include <string.h>4#include <stddef.h>5#include <stdbool.h>6#include <limits.h>7#include <errno.h>8 9int main(void) {10int a[]={2,4,6};printf("%zu\n",sizeof a/sizeof a[0]);11return 0;12}Function Arguments Are Values
Ruby passes each argument as a reference to the caller's object, and the parameter is a new local name for it. Reassigning the parameter never changes the caller's variable, although a mutating call such as << on an Array argument changes the shared object. C…
Ruby1def change(n)2 n = 93end4x = 25change(x)6puts xC1#include <stdio.h>2#include <stdlib.h>3#include <string.h>4#include <stddef.h>5#include <stdbool.h>6#include <limits.h>7#include <errno.h>8static void change(int n){n=9;(void)n;}9int main(void) {10int x=2;change(x);printf("%d\n",x);11return 0;12}Addresses and Dereferencing
Every Ruby variable refers to an object, but Ruby never hands you an address to follow: object_id is an identifier, not something you can dereference. In C &object obtains an address and *pointer accesses the pointed-to object. Dereference only a valid…
Ruby1box = [2]2other = box3other[0] = 54puts box[0]C1#include <stdio.h>2#include <stdlib.h>3#include <string.h>4#include <stddef.h>5#include <stdbool.h>6#include <limits.h>7#include <errno.h>8 9int main(void) {10int n=2;int *p=&n;*p=5;printf("%d\n",n);11return 0;12}Parsing with Status Results
Ruby Integer("12x") raises ArgumentError, while "12x".to_i quietly returns 12, the numeric prefix. C library parsers use explicit status information. sscanf reports the number of successful assignments, but matching a prefix alone does not prove the entire…
Ruby1text = "12x"2puts text.match?(/\A[0-9]+\z/)C1#include <stdio.h>2#include <stdlib.h>3#include <string.h>4#include <stddef.h>5#include <stdbool.h>6#include <limits.h>7#include <errno.h>8 9int main(void) {10const char *s="12x";int ok=*s!=0;for(size_t i=0;s[i];i++)if(s[i]<'0'||s[i]>'9')ok=0;printf("%d\n",ok);11return 0;12}
Syllabus
10 chapters48 lessons1 project48 challenges99 quiz questionsCompile and Compute
6 lessonsDecisions and Loops
5 lessonsArrays and Text
5 lessonsFunctions and Interfaces
5 lessonsPointers and Structs
5 lessonsValidation and Text Buffers
5 lessonsOwned Dynamic Memory
5 lessonsTagged Data and Composition
6 lessonsProject: Stock Ledger
4 lessonsFinal Ruby Ports
2 lessonsFrequently asked questions
Who is C for Ruby Developers for?
For people who already write Ruby and want to learn C. Each lesson starts from Ruby you know and shows how C does the same thing, so you skip the beginner basics.
Do I need to know C already?
No. The course starts with your first C program. You need Ruby basics: variables, conditions, loops, functions and collections.
How many lessons does C for Ruby Developers have?
C for Ruby Developers contains 48 interactive lessons you can complete at your own pace.
What does C for Ruby Developers cover?
The course is organized into chapters covering: Compile and Compute, Decisions and Loops, Arrays and Text, Functions and Interfaces, and more.
Is C for Ruby Developers free?
Yes. Every course on Coddy is free to start. You can complete lessons in your browser without any downloads or signup.