Menu
CoddyTech
Course

C++ for Ruby Developers

Transfer Ruby experience to C++17: values, flow, functions, containers, headers, classes, virtual dispatch, RAII, and move ownership. Build a connected library desk.

Ruby and C++ side by side

The same program in Ruby and in C++, from the lessons of this course.

  1. Strings and Output

    A Ruby String is a mutable object that carries an encoding, so length counts characters while bytesize counts bytes. C++ std::string owns a mutable byte sequence; stream insertion formats values. Length counts bytes, not Unicode characters.

    Ruby
    1word = "café"2puts word.length3puts word.bytesize
    C++
    1#include <iostream>2#include <string>3#include <vector>4#include <array>5#include <map>6#include <set>7#include <algorithm>8#include <memory>9#include <sstream>10#include <stdexcept>11#include <utility>12
    13int main(){14std::string name="oak";std::cout<<name.size()<<"\n";15}
  2. Text Equality

    Ruby == asks for value equality while equal? asks for object identity. C++ operator behavior depends on the declared operand types. C++ std::string equality compares contents. Raw pointer equality compares addresses; user classes need a deliberate equality…

    Ruby
    1a = ["red"]2b = ["red"]3puts a == b4puts a.equal?(b)
    C++
    1#include <iostream>2#include <string>3#include <vector>4#include <array>5#include <map>6#include <set>7#include <algorithm>8#include <memory>9#include <sstream>10#include <stdexcept>11#include <utility>12
    13int main(){14std::string a="red",b="red";std::cout<<std::boolalpha<<(a==b)<<"\n";15}
  3. Fixed-size Arrays

    A Ruby Array grows with << and holds references to its objects; C++ std::array has a compile-time length and value-copy behavior. C++ std::array<T,N> is a fixed-size value type. Its size is part of its type, and normal copy duplicates its elements.

    Ruby
    1items = [4, 8]2items << 123puts items[-1]4p items[0, 2]
    C++
    1#include <iostream>2#include <string>3#include <vector>4#include <array>5#include <map>6#include <set>7#include <algorithm>8#include <memory>9#include <sstream>10#include <stdexcept>11#include <utility>12
    13int main(){14std::array<int,3> a{4,5,6};std::cout<<a.size()<<" "<<a.at(1)<<"\n";15}
  4. Value Copies

    Ruby assignment copies a reference, so two variables can name the same Array or String, and dup and clone make only a shallow copy: the new outer Array still points at the same inner objects. C++ vector copy copies its elements according to their types, so…

    Ruby
    1original = [[1]]2copy = original.dup3copy[0] << 24copy << [9]5p original6p copy.size
    C++
    1#include <iostream>2#include <string>3#include <vector>4#include <array>5#include <map>6#include <set>7#include <algorithm>8#include <memory>9#include <sstream>10#include <stdexcept>11#include <utility>12
    13int main(){14std::vector<std::vector<int>> a{{1}};auto b=a;b[0].push_back(2);b.push_back({9});15std::cout<<a[0].size()<<" "<<a.size()<<" "<<b[0].size()<<" "<<b.size()<<"\n";16}
  5. Functions and Returns

    Ruby methods declare no parameter or return types, and a method returns the value of its last expression even without the return keyword, so twice works for any object that responds to *. A wrong argument count or type is found only when the call runs. C++…

    Ruby
    1def twice(value)2  value * 23end4puts twice(4)5puts twice("ab")
    C++
    1#include <iostream>2#include <string>3#include <vector>4#include <array>5#include <map>6#include <set>7#include <algorithm>8#include <memory>9#include <sstream>10#include <stdexcept>11#include <utility>12int triple(int n){return 3*n;}13int main(){14std::cout<<triple(4)<<"\n";15}
  6. Streams and Exact Parsing

    Ruby offers two conversions: Integer(text) raises ArgumentError unless the whole string is a number (or returns nil with exception: false), while String#to_i reads a leading numeric prefix and quietly returns 0 when there is none. C++ streams represent…

    Ruby
    1word, number = "oak 7".split2puts "#{word}/#{Integer(number)}"3p "12px".to_i4p Integer("12px", exception: false)
    C++
    1#include <iostream>2#include <string>3#include <vector>4#include <array>5#include <map>6#include <set>7#include <algorithm>8#include <memory>9#include <sstream>10#include <stdexcept>11#include <utility>12
    13int main(){14std::istringstream in("oak 7");std::string word;int n;if(in>>word>>n)std::cout<<word<<"/"<<n<<"\n";15std::istringstream bad("12px");int m=0;std::string rest;if(bad>>m&&!(bad>>rest))std::cout<<"exact\n";else std::cout<<"rejected "<<m<<"\n";16}

Syllabus

10 chapters48 lessons1 project48 challenges99 quiz questions

More courses for Ruby developers

Learn C++ from another language

Frequently 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: C++ Files and Values, Conditions and Iteration, Standard Containers, Copies and Algorithms, 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.

Coddy programming languages illustration

Start C++ for Ruby Developers for free

Get started