Skip to content
Tutorials Center
My learning

Topics

Lesson 1 of 134 ยท Getting Started

Introduction to Ruby

Meet Ruby 4.0, the readable object-oriented language behind Rails. Learn its history, philosophy, where it runs and write real code in your browser.

Ruby is a general-purpose programming language that was designed with one unusual goal: to make the programmer happy. Its creator, Yukihiro Matsumoto (known everywhere as Matz), wanted a language that reads almost like plain English, where you can say what you mean without a pile of punctuation and ceremony. Ruby is dynamically typed, fully object-oriented, garbage collected and interpreted, and it runs the same way on Linux, macOS and Windows.

This course teaches modern Ruby: the language as it exists in Ruby 4.0 (released in December 2025, as Ruby releases always are, on Christmas Day). You do not have to install anything. Every example marked with a Run button executes on a real Ruby 4.0 interpreter on our server, so you can change the code and see what happens right away. This first lesson explains what Ruby is, where it came from, where people use it today and why so many programmers describe it as joyful.

A short history

Matz began working on Ruby in 1993 and released it publicly in 1995 in Japan. He was a fan of Perl and Smalltalk and wanted something "more powerful than Perl and more object-oriented than Python", as he later put it. The name follows Perl's gemstone naming: a ruby is a gem. English documentation arrived around 2000, and the language slowly gained a Western audience.

The big moment came in 2004, when David Heinemeier Hansson released Ruby on Rails, a web framework that showed how fast a small team could build a database-backed website in Ruby. Rails drove most of Ruby's growth for the next fifteen years, and companies such as GitHub, Shopify, Basecamp and Airbnb were built on it. Ruby is much more than Rails, though, as the next section shows.

Ruby 3.0 (December 2020) was the "Ruby 3x3" release: the goal was to make Ruby three times faster than Ruby 2.0 and to add tools for concurrency and optional type information. Releases 3.1 to 3.4 added a just-in-time compiler called YJIT, pattern matching, Data objects, the short block parameter it and a new parser called Prism. Ruby 4.0 builds on all of that.

Where Ruby is used

  • Web applications. Rails and the lighter Sinatra and Hanami frameworks. Rails remains one of the fastest ways to go from an idea to a working product.
  • Scripting and automation. Ruby is excellent for the small programs that glue other tools together: renaming files, processing logs, calling APIs.
  • DevOps and developer tools. Homebrew (the macOS package manager), Vagrant, Chef, Jekyll (a static site generator) and many Git tools are written in Ruby. Rake, its build tool, is also used outside Ruby projects.
  • Testing. Ruby has some of the most expressive testing libraries around (Minitest and RSpec), and people use them to test code written in other languages too.
  • Learning to program. Because the syntax is small and forgiving, Ruby is a good first or second language.

Ruby is not the right tool for everything. It is slower than compiled languages such as Go, Rust or C++, it is not the usual choice for mobile apps or browser code, and very CPU-heavy number crunching normally goes to other tools. For the work it was made for, which is expressive, maintainable code written by people, it is hard to beat.

The philosophy: optimize for people

Matz has said many times that Ruby is designed for programmer happiness. In practice that gives the language a few distinct traits.

Everything is an object. Numbers, strings, even nil and true are objects with methods. There are no "primitive" types that behave differently.

Ruby Everything is an object

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with gets), it gets the next line from this box.

Notice that 5.even? is a method call on the number 5 itself. The question mark is part of the method name, a convention for methods that return true or false.

Code reads like a sentence. Parentheses are optional in most method calls, there are no semicolons, and loops are usually written as methods that take a block of code.

Ruby Readable loops and conditions

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with gets), it gets the next line from this box.

if and unless can come after the statement they guard, which keeps simple conditions on one line. The #{name} inside the double-quoted string is interpolation: Ruby inserts the value of the expression.

Flexibility over rules. Ruby usually gives you several ways to say the same thing and trusts you to choose well. That freedom is a feature, and the Ruby community balances it with strong conventions about style, which you will pick up in this course. It also means the language lets you do powerful, risky things, such as changing built-in classes, so part of becoming good at Ruby is learning restraint.

Ruby is dynamically typed

You never declare a type in Ruby. A variable simply refers to an object, and the object knows its own class. The same variable can later refer to an object of a different class.

Ruby Dynamic typing at work

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with gets), it gets the next line from this box.

This makes code short and flexible. The price is that mistakes show up when the code runs, not before. Ruby reports them as exceptions, and reading them is a core skill that we practice throughout the course. Here is the most famous Ruby error, which every programmer meets within the first hour.

Ruby Meeting NoMethodError (fails on purpose)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with gets), it gets the next line from this box.

Ruby stops and prints main.rb:2:in '<main>': undefined method 'upcase' for nil (NoMethodError). Read it from left to right: the file and line number (main.rb:2), where it happened ('<main>' means the top level of the script), what went wrong (there is no upcase method for nil) and the exception class in parentheses. The real bug is one line earlier: name was nil, so the call could not work. You will learn to guard against that in Ternary, Safe Navigation and Short-Circuit and to handle errors properly in Exceptions.

The error message format with straight single quotes around '<main>' and method names was introduced in Ruby 3.4. Older tutorials show backticks and apostrophes (`<main>'). Same information, new look.

What is new in Ruby 4.0

You do not need to know the history of every release, but a few modern features show up throughout this course, so here is a preview.

  • it: a short name for the single parameter of a block, so numbers.map { it * 2 } replaces numbers.map { |n| n * 2 }.
  • Pattern matching: case/in takes data apart by its shape. See Pattern Matching.
  • Data.define: small immutable value objects. See Struct and Data.
  • Endless methods: def square(x) = x * x.
  • Set built in: you can use Set without a require.
  • Faster execution: YJIT (and the newer ZJIT experiment) compile hot code to machine code. See How Ruby Runs.
  • Ractors and Fibers for concurrency, and the Prism parser, which gives tools such as editors and linters a reliable view of Ruby code.
Ruby A taste of modern Ruby

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with gets), it gets the next line from this box.

Do not worry if some of this looks strange. By the end of the Control Flow chapter every line will make sense. For now, notice how little noise there is: no type declarations, no semicolons, no boilerplate for the Point class, and the case expression reads like a description of the data.

How to use this course

The lessons are ordered so each one builds on the previous ones: Getting Started (this chapter), Control Flow, Methods and Blocks, Collections, Object-Oriented Ruby, Metaprogramming, the Standard Library, Concurrency, Testing and Tooling, the Ruby ecosystem, and a set of projects. Every lesson ends with a short quiz and a summary.

A few tips for learning Ruby well:

  • Run every example, then change it. Break it on purpose and read the error. Ruby's error messages are among the most helpful of any language.
  • Type, do not paste. In your own editor, typing the code builds the muscle memory.
  • Use p and inspect whenever you are unsure what a value is. Ruby's p shows an unambiguous representation, for example "5" versus 5.
  • Keep the examples small. Ruby rewards experiments: ten lines are enough to test most ideas.

When you are ready to work on your own computer, the usual path is to install Ruby with a version manager, write a file ending in .rb and run it with ruby file.rb. How Ruby Runs shows the details, including the interactive shell irb, gems and Bundler.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Who created Ruby, and what was the main design goal?

    Matz created Ruby in the 1990s with programmer happiness as the central goal. DHH created Rails, a framework written in Ruby.
  2. 2. What does it mean that everything in Ruby is an object?

    Even 5 and nil are objects: 5.even? and nil.to_a are ordinary method calls. There are no primitive types that behave differently.
  3. 3. What happens when a Ruby program calls a method that does not exist on an object, such as nil.upcase?

    Ruby is dynamically typed, so problems like this appear while the program runs, as a NoMethodError exception with the file, line and a description.
  4. 4. Which line is valid modern Ruby that doubles every number in an array using the implicit block parameter?

    Inside a block with one parameter, it refers to that parameter (Ruby 3.4 and later). The first option is valid syntax but ends with a dangling each that returns an enumerator, not the doubled numbers.

Summary

  • Ruby was created by Matz in the 1990s to make programmers happy; Rails made it famous, but it is also used for scripting, tooling, testing and learning.
  • Everything is an object, and code reads close to plain English.
  • Ruby is dynamically typed: values carry their class, and mistakes appear as exceptions at run time.
  • Error messages tell you the file, line, place and exception class, for example NoMethodError on nil.
  • Modern Ruby includes it, pattern matching, Data.define, endless methods and a built-in Set; YJIT speeds up execution.
  • All examples in this course run on Ruby 4.0 right in your browser.

Next: Your First Ruby Program