Intro to Ruby

Designed for programmer happiness. Elegant, expressive, and beautiful code.

1

The Story of Ruby

Ruby was born from one programmer's belief that coding should make you happy. Not just productive. Happy.

"I wanted a scripting language that was more powerful than Perl, and more object-oriented than Python."

Yukihiro "Matz" Matsumoto, creator of Ruby
1995
Matz releases Ruby in Japan. Named after the gemstone (a colleague's birthstone). He chose it because gems follow pearls, and Perl was the language he wanted to surpass.
2000
First English book published. Ruby spreads beyond Japan and gains an international community.
2004
Ruby on Rails is released by David Heinemeier Hansson. It revolutionizes web development with "convention over configuration." You could build a working web app in 15 minutes.
2006
Ruby explodes in popularity. Twitter, GitHub, Shopify, and Basecamp are all built with Rails. Startups everywhere adopt it.
Today
Still thriving. Powers Shopify ($200B+), GitHub, Airbnb, Twitch, and thousands of production apps worldwide.

Companies built with Ruby:

GitHub Shopify Airbnb Twitch Basecamp Kickstarter Hulu
2

Hello, World!

Ruby's Hello World is one of the simplest of any programming language. One word, one string, done.

hello.rb
puts "Hello, World!"

That's it. No imports, no main function, no semicolons. puts prints a line. Ruby gets out of your way.

No variable declarations needed. Just assign and go.
name = "Ruby"
year = 1995
puts "#{name} was created in #{year}"
# => Ruby was created in 1995

String interpolation with #{} lets you embed expressions directly inside strings. No concatenation needed.

irb (Interactive Ruby)
$ irb
irb> 2 + 2
=> 4
irb> "hello".upcase
=> "HELLO"
irb> "hello".reverse.upcase
=> "OLLEH"
irb> exit

irb is Ruby's REPL. Type an expression, get the result instantly. The => shows the return value of every expression.

3

Everything Is an Object

In most languages, numbers are just numbers. In Ruby, numbers are objects. Strings are objects. Even true and nil are objects. Everything has methods.

Numbers

5.times { puts "hi" } -42.abs 3.even?
Numbers have methods like .times, .abs, .even?, .between?

Strings

"hello".reverse "hello".length "hello".include?("ell")
Strings have 100+ built-in methods. No imports needed.

Arrays

[3, 1, 2].sort [1, 2, 3].sum [1, 2, 3].sample
Arrays come with powerful methods for sorting, filtering, and transforming.

Even nil

nil.class nil.nil? nil.to_s
nil is an object too. It's an instance of NilClass with its own methods.

This is what "pure object-oriented" means. You never have to wonder if something is an object. It always is.

Method chaining: the beauty of Ruby

1.upto(10)
  .select(&:odd?)
  .map { |n| n * n }
=> [1, 9, 25, 49, 81]

Count from 1 to 10, keep only odd numbers, square each one. Reads almost like English.

4

Blocks and Iterators

Blocks are Ruby's most distinctive feature. They let you pass a chunk of code to a method. Instead of writing loops, you tell collections what to do with each item.

Single-line blocks { }

# Double every number
[1, 2, 3].map { |n| n * 2 }
=> [2, 4, 6]

Multi-line blocks do...end

[1, 2, 3].each do |n|
  puts "Number: #{n}"
end

The essential iterators:

fruits = ["apple", "banana", "cherry"]

# .each - do something with each item
fruits.each { |f| puts f }

# .map - transform each item into something new
fruits.map { |f| f.upcase }
=> ["APPLE", "BANANA", "CHERRY"]

# .select - keep items that match a condition
fruits.select { |f| f.length > 5 }
=> ["banana", "cherry"]

# .reject - remove items that match a condition
fruits.reject { |f| f.start_with?("b") }
=> ["apple", "cherry"]
Why blocks matter: In most languages, you write a for-loop and manage an index variable. In Ruby, you describe what you want. .select means "keep the ones where this is true." .map means "transform each one like this." The code reads like your intention.
5

What Makes Ruby Special

Ruby's philosophy is simple: optimize for developer happiness. Every design decision asks, "Does this make the programmer's life better?"

Developer Happiness

Ruby was designed so programmers enjoy writing code. Readable syntax, intuitive naming, minimal boilerplate. The language respects your time.

Multiple Ways to Solve

There's more than one way to do things. Use unless instead of if-not. Use until instead of while-not. Pick the style that reads best for your situation.

Convention Over Configuration

Rails popularized this idea: sensible defaults mean you write less configuration and more actual code. Follow the convention, skip the setup.

The Gem Ecosystem

RubyGems hosts 170,000+ libraries (called "gems"). Need authentication? There's a gem. Payments? Gem. PDF generation? Gem. One command installs it.

Ruby on Rails

The framework that put Ruby on the map. Full-stack web development with database migrations, routing, templating, and testing built in. Startups love it.

Testing Culture

Ruby has the strongest testing culture of any language. RSpec, Minitest, and Capybara make writing tests feel natural, almost like writing documentation.

"Ruby is designed to make programmers happy." - Matz

This is not a joke or a marketing slogan. It is the actual design principle behind every feature in the language. When there is a trade-off between machine efficiency and programmer happiness, Ruby chooses happiness.
# Ruby reads like English
puts "You're old enough" if age >= 18

puts "Too young" unless age >= 18

coffee = if morning then "espresso" else "decaf" end

3.times { puts "Ruby!" }
6

Try It Yourself

Open the terminal and start writing Ruby. Here's how to get going in under a minute.

1
Install Ruby
apt install ruby
2
Create a file
nano hello.rb
3
Write some code
Type your Ruby code, then save with Ctrl+S and exit with Esc.
4
Run it
ruby hello.rb
5
Or use the REPL
irb opens an interactive session. Type expressions, see results instantly.

Mini-project: Number Guessing Game

guessing_game.rb
secret = rand(1..100)
guesses = 0

puts "I'm thinking of a number between 1 and 100."

loop do
  print "Your guess: "
  guess = gets.to_i
  guesses += 1

  if guess == secret
    puts "You got it in #{guesses} guesses!"
    break
  elsif guess < secret
    puts "Too low!"
  else
    puts "Too high!"
  end
end

Save this as guessing_game.rb and run it with ruby guessing_game.rb. Notice how readable the code is, even if you have never seen Ruby before.

Rubyist!

You've explored the language designed for programmer happiness. Ruby proves that code can be powerful and beautiful at the same time. Go write something elegant.

0
Sections Explored
0
Time Exploring

Everything Is an Object

Numbers, strings, booleans, even nil. In Ruby, everything you touch is an object with methods you can call.

Blocks Are Everywhere

Blocks let you pass behavior to methods. They make Ruby code concise, expressive, and surprisingly readable.

Rails Changed the Web

Ruby on Rails (2004) proved you could build web apps in days, not months. It shaped how modern frameworks work.

Optimized for Joy

Ruby was built so programmers could be happy. Readable syntax, multiple ways to solve problems, and a community that cares about craft.

Ready to Create?

Put your new knowledge into practice!

Suggest a Correction