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 in main and links compiled definitions; Ruby executes top-level statements and uses require_relative to load neighboring files. C++ stream insertion needs explicit newline output, while Ruby puts appends a newline. Ruby interpolation and…
C++1#include <iostream>2#include <string>3 4int main() {5 std::string name="Ada"; std::cout << "Hello, " + name << "\n";6}Ruby1print "oak!"2# A real comment3puts "#seed"Conditional Values
C++ if is a statement and the conditional operator selects a value. Ruby if itself yields the selected branch value, which can be assigned to a name. Ruby also supports postfix conditionals. Remember that zero is truthy in Ruby even though it converts to…
C++1#include <iostream>2#include <string>3 4int main() {5bool wet=false;std::string label=wet?"rain":"sun";std::cout<<label<<"\n";6}Ruby1wet = false2icon = if wet3 "umbrella"4else5 "sun"6end7puts iconArrays and Indexing
C++ vector grows dynamically, but operator[] requires a valid index and at performs checked access. Ruby arrays return nil for ordinary missing reads, and negative indexes count from the end. C++ size types and negative subscripts do not provide that Ruby…
C++1#include <iostream>2#include <vector>3#include <stdexcept>4 5int main() {6std::vector<int> values{4,8};std::cout<<values.back()<<"\n";7try{std::cout<<values.at(9)<<"\n";}catch(const std::out_of_range&){std::cout<<"missing\n";}8}Ruby1values = [4, 8]2puts values[-1]3puts(values[9].nil? ? "missing" : values[9])Mutation and Aliases
Copying a C++ vector of integers creates independent element storage, while a reference aliases the original vector. Ruby array assignment aliases the same object, and reverse! changes what every alias sees. Ruby reverse returns a new outer array. Distinguish…
C++1#include <iostream>2#include <vector>3 4int main() {5 std::vector<int> a{2};auto b=a;b[0]=8;std::cout<<a[0]<<":"<<b[0]<<"\n";6}Ruby1values = [1, 2, 3]2alias_values = values3values.reverse!4puts alias_values.join(" ")Modules as Namespaces
C++ namespaces organize compile-time names, and headers supply declarations for compiled translation units. Ruby modules organize constants and methods at runtime, while require_relative loads files. A filename does not automatically become a Ruby namespace.…
C++1#include <iostream>2namespace Helpers { int double_value(int x){return 2*x;} }3 4int main() {5 std::cout<<Helpers::double_value(4)<<"\n";6}Ruby1module TextTools2 def self.shout(text)3 text.upcase4 end5end6puts TextTools.shout("elm")Composing Objects
C++ composition may store values, references or smart pointers, each with distinct ownership and lifetime rules. Ruby instance variables retain references to collaborators without automatically cloning them. Delegation allows behavior to vary without…
C++1#include <iostream>2struct Policy {int factor;int apply(int n)const{return factor*n;}};3struct Service {const Policy& policy;int run(int n)const{return policy.apply(n);}};4 5int main() {6 Policy p{2};Service s{p};std::cout<<s.run(3)<<"\n";7}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.