Learn Ruby – tutorial, cheatsheet & exercises
Ruby was designed in 1995 by Yukihiro "Matz" Matsumoto – with the stated goal of making programmers happy. The code reads almost like prose, parentheses and semicolons are mostly unnecessary, and everything is an object: even numbers (5.times), nil and classes.
Ruby became famous through Ruby on Rails, the framework behind GitHub, Shopify and Basecamp. But Ruby also shines beyond that for scripts, automation (Homebrew, Vagrant, Fastlane) and prototypes. Especially elegant: blocks and the Enumerable module, which let you handle data processing in a few, easily readable lines.
Hello World in Ruby
puts "Hello World!"puts prints text with a line break. No main, no parentheses and no semicolon needed. Run it with ruby datei.rb.
How to get started with Ruby
A Mac ships with a system Ruby, but it is outdated and reserved for system purposes. Instead, install a current version (Ruby 3.3 or newer) with a version manager such as rbenv:
brew install rbenv ruby-build
rbenv install --list # show available versions
rbenv install 3.4.1 # or another current version
rbenv global 3.4.1
ruby --versionOn Windows you use the RubyInstaller (rubyinstaller.org), on Linux sudo apt install ruby-full or rbenv as well. You will find all downloads and instructions on ruby-lang.org.
You run a file with ruby. To experiment you use irb, the interactive shell (read-eval-print loop): you type an expression and immediately see the result.
ruby hallo.rb
ruby -e 'puts 2 + 3'
irbri Array#map shows the built-in documentation. With bundle init and bundle install you manage dependencies per project (see the lesson on gems and Bundler).sudo gem install. Use rbenv (or asdf/mise), then everything lives in your home folder.Ruby learning path
Hello World, puts & irb
Your first Ruby program and the idea "everything is an object".
Variables, Types & Strings
Local variables, numbers, strings, interpolation and constants.
Conditions: if, unless, case
Deciding with if/elsif/else, the modifier form and the powerful case/when.
Loops and Iterators
times, each, upto, step, while, until and loop.
Defining Methods
def, default values, keyword arguments, splat and the implicit return value.
Arrays and Hashes
The two most important collections and their most common pitfalls.
Blocks, Procs & Lambdas
The heart of Ruby: passing code as an argument, yield and closures.
Symbols & Strings in Detail
Symbols vs. strings, important string methods, heredocs and frozen strings.
Enumerable: map, select, reduce & Co.
Processing data without loops – with the methods that arrays, hashes and ranges share.
Classes & Objects
Attributes, constructor, inheritance, visibility and comparisons.
Modules, Mixins & Comparable
include, extend, prepend, namespaces as well as Enumerable and Comparable for your own classes.
Struct, Data & Pattern Matching
Quick data classes with Struct, immutable ones with Data and pattern matching with case/in.
Errors, Files & JSON
begin/rescue/ensure, custom error classes, retry, the File API and JSON.
Gems, Bundler & Tests
Installing libraries, managing dependencies per project and testing code.
Metaprogramming & DSLs
method_missing, define_method, open classes, refinements and self-built DSLs.
Ruby on Rails: the Basic Idea
MVC, convention over configuration, ActiveRecord and the typical flow of a Rails app.
At a glance
More about Ruby
Test & practice
Interactive quiz, timed exam with certificate, and exercises with an editor: