RbRuby

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.

Web (Rails)ScriptingPrototypes

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 --version

On 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'
irb
TipIn the regular terminal (not inside irb), ri 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).
WarningNever install gems into the system Ruby with sudo gem install. Use rbenv (or asdf/mise), then everything lives in your home folder.

Ruby learning path

Beginner
Intermediate
Advanced

At a glance

First released1995
CreatorYukihiro Matsumoto
ParadigmObject-oriented, functional
TypingDynamic, strong
ExecutionInterpreted
File extension.rb

Test & practice

Interactive quiz, timed exam with certificate, and exercises with an editor:

Ruby compared

Hello World: Ruby comparedVariables: Ruby comparedConditionals: Ruby comparedLoops: Ruby comparedFunctions: Ruby comparedLists and Arrays: Ruby comparedDictionaries and Maps: Ruby comparedClasses and Objects: Ruby comparedError Handling: Ruby comparedReading Files: Ruby compared