Getting Started

Ruby Fundamentals — Getting Started
Ruby Fundamentals
Course 1 · Chapter 1 · Getting Started

💎 Getting Started with Ruby

Ruby is an interpreted, dynamically-typed, thoroughly object-oriented language created by Yukihiro "Matz" Matsumoto in the mid-1990s, explicitly designed around programmer happiness and readability rather than raw performance. This chapter covers what makes Ruby distinct from PHP and Python — the two languages you already know — and the absolute basics: running a script, the interactive REPL, and printing output.

💡 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 if and case) return a value, which shows up constantly once you start writing idiomatic Ruby

Ruby vs PHP vs Python, at a Glance

TraitPHPPythonRuby
File extension.php.py.rb
Primitivesint/string/bool are not objectsMostly objects, some exceptionsEverything is an object, no exceptions
Run a scriptphp script.phppython script.pyruby script.rb
Interactive shellphp -a (rarely used)python (REPL)irb (central to the workflow)
Statement terminatorSemicolon requiredNewline (no semicolons)Newline (semicolons optional)
Block delimiters{ }Indentationdo...end or { }, plus end keywords

▶️ Your First Ruby Program

Hello, Ruby

# hello.rb puts "Hello, Ruby!"

Run it from the command line:

$ ruby hello.rb Hello, Ruby!

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.

$ irb irb(main):001:0> 2 + 2 => 4 irb(main):002:0> "Ruby".upcase => "RUBY" irb(main):003:0> exit

📣 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 "Hello" # Hello\n -- adds a newline automatically print "Hello" # Hello -- no trailing newline p "Hello" # "Hello" -- shows the inspect form, quotes included puts [1, 2, 3] # prints each element on its own line: 1 / 2 / 3 p [1, 2, 3] # prints the array literally: [1, 2, 3]

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

# A single-line comment, same symbol as Python =begin This is a multi-line block comment. Ruby is one of the few languages with a dedicated block-comment syntax rather than reusing single-line comments. =end puts "Comments don't affect output" # inline comments work too

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:

5.class # => Integer 5.even? # => true -- calling a method directly on an integer literal nil.class # => NilClass -- even nil is an object with a class nil.to_s # => "" -- nil responds to methods like anything else "ruby".class # => String
⚠ In PHP/Python, this would look very different

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.

→ Solution

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.

→ Solution

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.

→ Solution

💎 Why "Programmer Happiness" Isn't Just Marketing

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.