Getting Started
💎 Getting Started with Ruby
💡 What Is Ruby, and Why Learn It?
Ruby is:
- Interpreted and dynamically typed — like PHP and Python, no compile step, types are checked at runtime
- Purely object-oriented — unlike PHP and Python, there are no primitives at all in Ruby. Every value, including numbers and
nil, is an object you can call methods on - Designed for programmer happiness — Matz has been explicit that Ruby prioritizes how enjoyable and readable the language feels to write, sometimes over strict consistency
- The engine behind Ruby on Rails — the web framework that made Ruby famous; this course covers the language itself, with Rails as a natural follow-up
- Expression-oriented — most Ruby constructs (including
ifandcase) return a value, which shows up constantly once you start writing idiomatic Ruby
Ruby vs PHP vs Python, at a Glance
| Trait | PHP | Python | Ruby |
|---|---|---|---|
| File extension | .php | .py | .rb |
| Primitives | int/string/bool are not objects | Mostly objects, some exceptions | Everything is an object, no exceptions |
| Run a script | php script.php | python script.py | ruby script.rb |
| Interactive shell | php -a (rarely used) | python (REPL) | irb (central to the workflow) |
| Statement terminator | Semicolon required | Newline (no semicolons) | Newline (semicolons optional) |
| Block delimiters | { } | Indentation | do...end or { }, plus end keywords |
▶️ Your First Ruby Program
Hello, Ruby
Run it from the command line:
No class wrapper, no public static void main-style boilerplate, no build step — a Ruby file is just a sequence of statements executed top to bottom, the same way a PHP or Python script runs. puts is a top-level method available everywhere by default, not something you import.
🖥️ irb — The Interactive Ruby Shell
Ruby ships with irb (Interactive Ruby), a REPL (read-eval-print loop) you launch by typing irb at the command line. It plays a much bigger role in everyday Ruby work than PHP's rarely-used php -a: it's the standard way to quickly test an expression, inspect an object's available methods, or experiment with syntax before writing it into a file — and it's the same underlying tool Rails' rails console is built on, so getting comfortable with it now pays off directly later.
📣 puts, print, and p — Three Ways to Output
Where PHP has echo/print and Python has a single print(), Ruby gives you three distinct output methods, each with a different job:
puts — for humans
Adds a trailing newline, and if you give it an array, prints each element on its own line. This is the everyday "show the user something" method — closest to PHP's echo with a manual "\n" already built in.
p — for debugging
Prints the inspect form of an object — strings show their quotes, arrays show their brackets exactly as written. This is the method you reach for when you want to see the real shape of a value, similar in spirit to Python's repr().
💬 Comments
Ruby's single-line # matches Python exactly (PHP supports # too, though // is more common there). The =begin/=end block form is unique to Ruby — PHP and Python both reuse other syntax (/* */, triple-quoted strings) instead of having a true dedicated block-comment construct.
🧬 Everything Is an Object
This is the single biggest philosophical difference from PHP and Python, and it shows up immediately:
In PHP, 5 is a scalar, not an object — you can't call a method directly on an integer literal at all. Python is closer (integers are objects, and (5).bit_length() works), but Python's None and Ruby's nil behave differently: calling an arbitrary method on None raises an AttributeError, while nil in Ruby happily responds to a defined set of its own methods, like nil.to_s above. Keep this distinction in mind — it'll matter again once method chaining shows up.
💻 Coding Challenges
Challenge 1: puts vs print vs p
Write a Ruby script that creates a string variable and an array variable, then prints each one using all three of puts, print, and p — six lines of output total. Add a comment above each line explaining, in your own words, what's different about the output.
Goal: Build a clear mental model of when to reach for each of Ruby's three output methods.
Challenge 2: Comments & Running a Script
Create a file called profile.rb. Using both single-line # comments and an =begin/=end block comment, document a short script that prints your name, a favorite number, and whether that number is even or odd (using .even?). Run it from the command line with ruby profile.rb.
Goal: Get comfortable writing, saving, and running a real .rb file rather than only experimenting in irb.
Challenge 3: Everything Is an Object
In irb, call at least four different methods directly on literals with no variable assignment — for example 10.class, "hello".length, nil.to_a, true.class. Write down each result, then write one sentence explaining how this would need to be written differently in PHP.
Goal: Internalize that in Ruby, literals aren't a special case — they're objects like anything else.
You'll notice Ruby often gives you several ways to do the same thing (puts/print/p is the first example this chapter covers, but it's a pattern that recurs throughout the language). That's a deliberate design choice, not an inconsistency — Matz optimized for the syntax reading naturally in context over having exactly one rigid way to do something. Coming from PHP and Python's generally stricter "one obvious way" philosophy, expect Ruby to feel more flexible, and occasionally more ambiguous, by design.
🎯 What's Next
Next chapter: Variables & Data Types — symbols (a Ruby-specific type with no direct PHP/Python equivalent), strings, numbers, the basics of arrays and hashes, and how nil fits into all of it.