Lumenary CS · Programming languages
Ruby
A language designed for programmer happiness, where everything is an object and blocks make code read like prose, from a first script to idiomatic, tested, professional Ruby.
Ruby is a dynamic, object-oriented programming language created in Japan by Yukihiro "Matz" Matsumoto and first released in 1995. Its stated aim is to make programmers productive and happy: everything in Ruby is an object, blocks let you pass behaviour to methods as naturally as you pass values, and the standard library is rich and consistent. Ruby is best known for Ruby on Rails, the web framework behind many well-known web applications and countless smaller ones, and it is also used for command-line tools, automation, infrastructure software and data processing.
This course is for anyone who already knows what a variable, a loop and a function are: the Start level of Lumenary CS or equivalent experience in any language. It begins with Ruby's philosophy and your first scripts, works through objects, control flow, methods and blocks, collections and the Enumerable module, classes and mixins, metaprogramming, pattern matching and error handling, and ends with concurrency (threads, fibers and Ractors), gems, Bundler and testing, an overview of Rails, and a complete small project.
To run the code, install Ruby by following the official guide at ruby-lang.org (a version manager such as rbenv or mise on macOS and Linux, RubyInstaller on Windows), or use the official Docker image ruby. You can also try Ruby in the browser at TryRuby. Every complete program in these lessons was run with Ruby 4.0 by Lumenary's test suite, and the output shown is the output it printed. The lessons were drafted with AI assistance and are awaiting review by a human computer scientist.
- Chloe · The Designer
“Why is the language built this way?” - Margaret · The Profiler
“What does it print, and what does it cost?” - Gideon · The Language Lawyer
“What exactly does the language guarantee?”
Foundations · 4 lessons
Foundations
Install Ruby and meet its philosophy, then learn the core language: objects, numbers, strings and symbols, control flow, methods, blocks, procs and lambdas, and the collections that Enumerable makes so expressive.
- 1Getting started with RubyWhere Ruby came from and what it values, how to install and run it, and a first look at a language in which everything is an object. 45 min
- 2Values, strings, symbols and control flowRuby's numbers, its Unicode-aware and (soon) frozen strings, symbols, the two falsy values, and the expressions that make decisions and repeat work. 50 min
- 3Methods, blocks, procs and lambdasDefining methods with positional and keyword arguments, and Ruby's signature feature, the block, with its relatives the proc and the lambda. 55 min
- 4Collections and EnumerableArrays, hashes, ranges and sets, and the Enumerable module that gives every collection the same rich vocabulary of transforming, filtering, grouping and searching. 60 min
Practice · 3 lessons
Practice
Design with classes, modules and mixins, bend the language with metaprogramming (and know when not to), and handle data and failure with pattern matching and exceptions.
- 5Classes, modules and mixinsDefining classes with state and behaviour, inheritance and super, modules as namespaces and as mixins, the method lookup chain, and value objects with Struct and Data. 60 min
- 6MetaprogrammingCode that writes code — dynamic method calls and definitions, method_missing, open classes and refinements, hooks and class macros — and the judgement to use them sparingly. 55 min
- 7Pattern matching and error handlingTaking data apart by its shape with case/in, and dealing with failure through Ruby's exceptions — raise, rescue, else, ensure, retry and your own error classes. 60 min
Professional · 2 lessons
Professional
Do several things at once with threads, fibers and Ractors, and work the way Ruby teams do: gems, Bundler, Minitest, RuboCop, an overview of Rails, and a complete project.
- 8Concurrency: threads, fibers and RactorsHow Ruby does several things at once — threads under the global VM lock, fibers and the fiber scheduler, parallel Ractors, and processes — and which to choose for which job. 60 min
- 9Gems, testing, Rails and a complete projectHow Ruby teams work every day — RubyGems and Bundler, Minitest and RSpec, RuboCop, debugging and type checking, an overview of Rails — and a small, complete, tested program. 70 min
Further reading and resources
- ruby-lang.orgThe official home of Ruby: downloads, installation, news and release announcements.
- Ruby documentationThe official reference for the core classes, the standard library and the language syntax, for each Ruby version.
- Ruby language specification (ISO/IEC 30170)An international standard for Ruby, published in 2012 and based on Ruby 1.8 and 1.9. Today the reference implementation, CRuby, and its documentation define the language in practice.
- Ruby NEWSWhat changed in the latest Ruby release, maintained in the Ruby source repository.
- TryRubyThe official interactive tutorial and playground that runs Ruby in your browser.
- RubyGems.orgThe community's package repository.