A language designed for people
Most programming languages are designed around what machines need: speed, memory layout, how the processor executes instructions. Ruby was designed around what programmers need. Its creator, Yukihiro Matsumoto, known everywhere as Matz, has said many times that his main goal was to make programming productive and enjoyable, so that programmers can focus on the creative part of the work. That goal explains most of Ruby's character:
- Everything is an object. Numbers, strings,
nil, classes, even blocks of code can be objects with methods. There are no special cases to remember. - Code reads like a description of what it does.
3.times { … },names.sort_by(&:length),return unless user.admin?read almost as English. - Blocks let you hand a piece of code to a method as easily as you hand it a number, which makes iteration, resource handling and small domain-specific languages feel natural.
- Flexibility. You can reopen classes, define methods at run time and build expressive libraries. That power is also a responsibility, which this course takes seriously.
Ruby is a dynamic, interpreted language, slower than compiled languages like Go or Rust for raw number crunching, and it makes no secret of trading some speed for expressiveness. In practice most applications spend their time waiting for databases and networks, and Ruby's modern just-in-time compilers have closed much of the gap.
Ruby is best known for Ruby on Rails, the web framework that popularised conventions like "convention over configuration" and influenced web frameworks in many other languages. GitHub, Shopify and Basecamp are well-known Rails applications. Ruby is also used for command-line tools, automation and infrastructure (Homebrew, the macOS package manager, and the configuration tool Chef are written in it), static-site generators such as Jekyll, and testing tools.
From Matz's side project to Ruby 4.0
Matz started Ruby in 1993 in Japan, wanting a scripting language that was more truly object-oriented than Perl and more practical for everyday scripting than the pure object-oriented languages of the time. He borrowed freely: objects and blocks from Smalltalk, practical text handling from Perl, ideas from Lisp, Eiffel and Ada. The name is a gemstone, a nod to Perl (the pearl is the birthstone for June, the ruby for July). The first public release, version 0.95, appeared on Japanese newsgroups in December 1995, and version 1.0 followed a year later.
For several years Ruby was popular mainly in Japan. That changed in 2000, when Dave Thomas and Andy Hunt published Programming Ruby, the first English book about it, known as the "Pickaxe" from its cover. In 2004 David Heinemeier Hansson extracted a web framework from Basecamp, the project-management application he was building, and released it as Ruby on Rails. Rails made Ruby world-famous almost overnight.
Ruby's main implementation, called CRuby or MRI (Matz's Ruby Interpreter), has changed a great deal since then. Ruby 1.9 (2007) replaced the original interpreter with YARV, a bytecode virtual machine by Koichi Sasada, and made strings aware of their encoding. Ruby 2.0 (2013) added keyword arguments and refinements. Ruby 3.0 (2020) came out of a project called "Ruby 3x3", which aimed to make Ruby three times faster than 2.0, and added Ractors for parallelism, a fiber scheduler for concurrent I/O, and RBS type signatures; pattern matching, introduced experimentally in 2.7, became stable. YJIT, a just-in-time compiler developed at Shopify, arrived in 3.1. Ruby 3.4 (2024) introduced it for simple blocks and switched to the new Prism parser.
Ruby 4.0 was released on 25 December 2025, thirty years after the first public release. It brought a new experimental JIT compiler called ZJIT, an experimental isolation feature called Ruby::Box, a reworked Ractor API, and made Set a core class. New Ruby versions are released every year on 25 December, a tradition the community looks forward to. This course was tested with Ruby 4.0 and marks features that need a recent version.
Other implementations exist: JRuby runs on the Java virtual machine, TruffleRuby on GraalVM, and mruby is a small Ruby for embedding in other programs. Everything in this course is about CRuby, which is what almost everyone uses.
Installing Ruby and running a first program
The official installation guide lists every option. In short:
- macOS and Linux: use a version manager such as
rbenv,chruby,asdformise. Operating systems often ship an old Ruby that the system itself depends on, and projects pin their own Ruby version (in a.ruby-versionfile), so being able to install several side by side is the professional norm. - Windows: use RubyInstaller, from rubyinstaller.org, or use Ruby inside WSL.
- Docker:
docker run --rm -it ruby irbgives you an interactive Ruby without installing anything.
Then check:
ruby --version
irb # Interactive Ruby: type expressions, see their values
ruby -e 'puts 6 * 7' # run a one-line program
ruby hello.rb # run a fileRuby files end in .rb. There is no main function and nothing to compile: Ruby runs the file from top to bottom.
# A first Ruby program. Run it with: ruby hello.rb
name = "Amara"
first_release = 1995
puts "Hello, #{name}!"
puts "Ruby was first released in #{first_release}, #{2026 - first_release} years before 2026."
puts 'Single quotes print #{name} as it is.'
print "print adds no newline, ", "so this continues the line.\n"
p name
p [1, "two", :three, nil, 4.5]
puts [1, "two", :three, nil, 4.5]Hello, Amara!
Ruby was first released in 1995, 31 years before 2026.
Single quotes print #{name} as it is.
print adds no newline, so this continues the line.
"Amara"
[1, "two", :three, nil, 4.5]
1
two
three
4.5What each line does:
- Variables need no declaration and no sigil:
name = "Amara"creates one. Local variables and method names usesnake_case. - String interpolation puts the value of any Ruby expression into a double-quoted string:
"#{2026 - first_release}"computes the number and converts it to text. Single-quoted strings are taken literally. putsprints its arguments, each followed by a newline. Given an array, it prints each element on its own line, andnilbecomes an empty line, as the last five lines of output show.printadds no newline.pprints the inspected form of a value, the way a programmer would write it: strings in quotes, symbols with a colon,nilasnil. It is the quickest debugging tool in Ruby;pp("pretty print") does the same with nicer layout for large structures.- Comments start with
#. - There are no semicolons and, for method calls, the parentheses are optional:
puts("Hello")andputs "Hello"are the same. Ruby style leaves them out for simple statements likeputsand keeps them where they help reading.
Everything is an object
In many languages, numbers and null are special values that are not objects. In Ruby there is no such division. Every value is an object, every object belongs to a class, and you interact with every object by calling its methods:
# Every value is an object, and every object has a class.
[42, 3.14, "hi", :hi, nil, true, 1..3, [1, 2], {a: 1}].each do |value|
puts "#{value.inspect.ljust(8)} #{value.class}"
end
# Classes are objects too.
puts Integer.class, Integer.ancestors.take(4).inspect
# So you call methods on numbers, nil and everything else.
3.times { |i| print i, " " }
puts
puts 7.even?, 7.succ, -7.abs, 10.gcd(4), 255.to_s(2), 3.14159.round(2)
puts "lumenary".capitalize.reverse
puts 2**100
puts nil.to_a.inspect, nil.to_s.empty?
# Even + is a method: 5 + 3 is sugar for 5.+(3).
puts 5.+(3), 5.public_send(:*, 4), 5.respond_to?(:upcase)42 Integer
3.14 Float
"hi" String
:hi Symbol
nil NilClass
true TrueClass
1..3 Range
[1, 2] Array
{a: 1} Hash
Class
[Integer, Numeric, Comparable, Object]
0 1 2
false
8
7
2
11111111
3.14
yranemuL
1267650600228229401496703205376
[]
true
8
20
falseSome things to notice:
42.classisInteger,nil.classisNilClass, and evenInteger.classisClass. Classes are objects too, which is what makes Ruby's metaprogramming possible later in the course.Integer.ancestorslists the classes and modules Ruby searches for an integer's methods:Integer, thenNumeric, then the moduleComparable(which supplies<,between?andclamp), thenObject. You will meet this lookup chain properly in the lesson on classes and modules.3.times { |i| … }is a method call on the number 3, with a block (the code in braces) that the method runs three times, passing 0, 1 and 2 asi. This is how Ruby loops most of the time.- Predicate methods end in
?and return true or false, by convention:7.even?,"".empty?,5.respond_to?(:upcase). - Integers have no size limit.
2**100is computed exactly; Ruby switches to a big-number representation automatically. - Operators are methods.
5 + 3is sugar for calling the method+on 5 with the argument 3, so5.+(3)works, andpublic_send(:*, 4)calls*by name. Your own classes can define+,==,<=>or[]and work with the same syntax as built-in types. nilis an object too, with methods liketo_aandto_s. That does not make it safe to ignore: calling a methodnillacks raisesNoMethodError, Ruby's most common runtime error.
Here is a small command-line script in the style you will write throughout the course:
def greet(name, greeting: "Hello") = "#{greeting}, #{name}!"
names = ARGV.empty? ? %w[Wanjiru Tomás Mei] : ARGV
names.each.with_index(1) do |name, position|
puts "#{position}. #{greet(name)}"
end
puts greet("world", greeting: "Good morning")
puts "#{names.size} names; the longest is #{names.max_by(&:length)}"
puts "arguments given: #{ARGV.length}"1. Hello, Wanjiru!
2. Hello, Tomás!
3. Hello, Mei!
Good morning, world!
3 names; the longest is Wanjiru
arguments given: 0def greet(name, greeting: "Hello") = … is an endless method (Ruby 3.0), a one-line method whose body is a single expression; greeting: is a keyword argument with a default. %w[Wanjiru Tomás Mei] is shorthand for an array of words. ARGV holds the command-line arguments (without the script's name, unlike some languages), so ruby greet.rb Ana Kofi greets Ana and Kofi instead. each.with_index(1) numbers the elements from 1, and max_by(&:length) finds the longest name: &:length is shorthand for a block that calls length on each element. The next lessons explain each of these tools properly.
Reading Ruby: the conventions everyone follows
Ruby has few hard rules about naming, and a strong community culture about it. Learn these conventions now and every Ruby codebase you open will feel familiar:
| You see | It means |
|---|---|
total_price, send_reminder | local variables and methods: snake_case |
ShoppingCart, Net::HTTP | classes and modules: CamelCase; :: reaches inside a namespace |
MAX_RETRIES | a constant: starts with a capital letter, usually all capitals |
@balance | an instance variable, belonging to one object |
@@count, $stdout | a class variable and a global variable; both are rare in modern code |
:pending | a symbol, an immutable name (next lesson) |
empty?, valid? | a method that answers a yes-or-no question |
sort!, save! | a "dangerous" version of a method: it changes the object in place, or raises an exception where the plain version would return false |
name= | a setter, called as obj.name = "x" |
Indentation is two spaces, never tabs. Blocks use braces { … } when they fit on one line and do … end when they span several. Methods are short, often only a few lines, and return the value of their last expression without writing return.
When you want to know what a method does, the official documentation has every core class. Inside irb, typing an expression and pressing Tab completes method names, and show_doc String#center (or the ri command in a terminal) shows the documentation without leaving your shell, provided your Ruby was installed with its documentation (some minimal installations, including the official Docker image, leave it out to save space).
Common misunderstandings
Misconception
"Ruby is only for Rails." Rails is Ruby's most famous use, but Ruby is a general-purpose language with a large standard library. Many developers use it for scripts, command-line tools and automation without ever touching a web framework, and this course teaches the language first.
Misconception
"puts and p are interchangeable." puts is for output meant for people: it calls to_s, so puts nil prints an empty line and puts "5" and puts 5 look the same. p is for programmers: it calls inspect, so you can see nil, quotes around strings and the difference between "5" and 5. When debugging, use p.
Misconception
"Leaving out parentheses is sloppy." It is idiomatic Ruby for many calls, especially puts, require, raise and method calls that read like commands. But nested calls without parentheses become hard to read and can parse differently than you expect, so the community style is to use parentheses whenever a call has arguments and is part of a larger expression.
Misconception
"Ruby is too slow for real work." Ruby is slower than compiled languages for tight numeric loops, and you should not choose it for those. Large Ruby applications serve enormous amounts of traffic, because typical web work is dominated by waiting for databases and networks, and YJIT has made CPU-bound Ruby code substantially faster. Measure before deciding.
Exercises
1. Getting started with Ruby
Warm-upComputationalWhat does this program print?
word = "ruby"
puts "#{word.upcase}#{word.length}" + '#{word}'
2. Getting started with Ruby
Warm-upComputationalWhat does puts 10.gcd(4) + 7.succ print?
3. Getting started with Ruby
CoreHumanWhat is Integer.class?
4. Getting started with Ruby
CoreComputationalHow many lines of output does puts [1, nil, "two"] produce?
5. Getting started with Ruby
CoreComputationalA variable x might be nil, "" or "nil", and you want to see which while debugging. Which line shows the difference?
6. Getting started with Ruby
CoreComputationalWhat does puts 255.to_s(2).length print?
7. Getting started with Ruby
Warm-upHumanWhy do Ruby developers on macOS and Linux usually install Ruby with a version manager (rbenv, chruby, asdf or mise) rather than the operating system's Ruby?
Further reading and resources
- Installing RubyThe official guide to installers, version managers and package managers on every platform.
- About RubyThe Ruby project's own account of the language's ideas and history.
- Ruby from Other LanguagesWhat will feel familiar and what will not, for programmers coming from C, Java, Python, PHP and Perl.
- TryRubyAn official interactive tutorial that runs in your browser.
- Ruby documentationThe reference for every core class and standard library, by version.