Ruby Introduction
Ruby is a dynamic, object-oriented programming language built around one idea: code should be pleasant to write and easy to read. Its creator, Yukihiro "Matz" Matsumoto, has said many times that he designed Ruby to make programmers happy. The result is a language whose programs often read almost like English sentences.
Ruby was first released in Japan in 1995 and became famous worldwide in the mid-2000s thanks to the Ruby on Rails web framework. Today it powers sites such as GitHub, Shopify and Basecamp, and it is a favourite language for scripting, automation and quick prototypes.
What can Ruby do?
- Build web applications and APIs (Ruby on Rails, Sinatra, Hanami)
- Write command-line tools and automation scripts
- Process text, logs, CSV and JSON files
- Create internal tools, background jobs and data pipelines
- Describe configuration and build steps (Homebrew formulas, Rake tasks, Vagrant, CocoaPods)
- Write tests in a very readable style (Minitest, RSpec)
Why learn Ruby?
| Feature | What it means for you |
|---|---|
| Everything is an object | Even 5 and nil have methods: 5.times, nil.to_a |
| Blocks | Pass a chunk of code to a method: [1, 2].each { |n| puts n } |
| Interpreted | Run a file directly, no compile step |
| Dynamically typed | No type declarations; values carry their own type |
| Expressive standard library | Hundreds of handy methods on strings, arrays and hashes |
| Friendly community | Strong culture of readable code, tests and good documentation |
Ruby versions
This course uses Ruby 3.4, the current stable line. Ruby 3 brought big improvements: pattern matching, keyword-argument cleanup, Data value objects and a faster interpreter with the YJIT compiler. Where a feature needs a recent version, the lesson says so.
Note: The playgrounds on ExcelTAD send your code to the Compiler Explorer service, which runs it on a real Ruby 3.4 interpreter and returns the output. You need an internet connection, and a run usually takes a second or two.
Your first Ruby program
A Ruby program can be a single line. There is no main function and no class to write first — the interpreter simply runs the file from top to bottom. Press ▶ Run:
puts "Hello, World!"Hello, World!
puts ("put string") prints a value followed by a newline. Notice that the parentheses are optional: puts("Hello") and puts "Hello" mean the same thing, and Rubyists usually leave them out when the line is clear.
Tip: Every playground is editable. Change the message and press Ctrl+Enter to run it again.
Everything is an object
In many languages numbers and text are "primitive" values with no behaviour of their own. In Ruby every value is an object, so you can call methods on anything with a dot:
puts "ruby".upcase
puts "ruby".length
puts 7.even?
puts 3.14.round
puts nil.to_a.inspect
puts 42.classRUBY
4
false
3
[]
Integer
Method names ending in ? (like even?) return true or false — a naming convention you will see everywhere in Ruby.
A taste of Ruby style
Here is a small program that shows why people enjoy writing Ruby. Don't worry about every detail yet; each piece has its own lesson later in the course.
fruits = ["kiwi", "apple", "mango", "fig"]
3.times { |i| puts "Round #{i + 1}" }
fruits.sort.each do |fruit|
puts "- #{fruit.capitalize}"
end
long_names = fruits.select { |f| f.length > 3 }
puts "Long names: #{long_names.join(', ')}"Round 1
Round 2
Round 3
- Apple
- Fig
- Kiwi
- Mango
Long names: kiwi, apple, mango
3.times { ... }repeats a block three times.#{...}inside double quotes inserts a value into a string (interpolation).selectkeeps only the items for which the block returns true.
Ruby compared with other languages
| Ruby | Python | JavaScript | |
|---|---|---|---|
| Print a line | puts "hi" |
print("hi") |
console.log("hi") |
| Block structure | do ... end / { } |
indentation | { } |
| Nothing value | nil |
None |
null / undefined |
| String interpolation | "#{x}" |
f"{x}" |
`${x}` |
| Loop over a list | list.each { |x| ... } |
for x in list: |
for (const x of list) |
What you will learn
The course follows a natural path: first the basics (variables, strings, numbers, conditions, loops, arrays and hashes), then methods and blocks, then object-oriented programming with classes and modules, and finally advanced topics such as exceptions, pattern matching, regular expressions, files, JSON, metaprogramming and testing. It ends with exercises and a small project.
Test yourself
Who created Ruby?
- Guido van Rossum
- Yukihiro Matsumoto
- Brendan Eich
- James Gosling
What does puts 7.even? print?
- true
- false
- 7
- An error, numbers have no methods
Which famous web framework made Ruby popular?
- Django
- Laravel
- Ruby on Rails
- Express