Skip to Content
Course content

1: Installing Ruby and Using irb

Click on the "Edit" button in the top corner of the screen to edit your slide content.

Before we start writing actual files and building apps, we need to make sure your machine actually speaks Ruby. I've spent way too many hours of my career fighting with "system Rubies"—the versions that come pre-installed on macOS or Linux—because they're usually outdated or restricted by permissions. Let's see where you stand.

Checking the pulse of the system

First, let's just see if Ruby is already hanging around. Open your terminal and type this:

ruby -v

If you see something like ruby 3.2.2, you're in a good spot. But if you get a "command not found" or a version from 2015, we have a problem. I personally never use the system Ruby; I use a version manager like rbenv or rvm. Why? Because eventually, you'll need one project to run on Ruby 2.7 and another on 3.3, and trying to manage that manually is a nightmare. If you're missing it, go ahead and install rbenv and the latest stable Ruby version. Once that's done, run ruby -v again. When you see that 3.x version number, we're ready to actually play.

The sandbox approach

Now, I could have you create a main.rb file, save it, and run it from the terminal, but that's a slow way to learn. It's too much friction. Instead, I want you to try irb. It stands for "Interactive Ruby," and it's what we call a REPL (Read-Eval-Print Loop). Basically, it's a playground where Ruby executes your code the second you hit enter.

Type irb in your terminal and hit enter. You should see a prompt like irb(main):001:0>. We're now "inside" Ruby.

Calculating a coffee tab

Let's use this to solve a real-world problem. Say you're buying coffee for four coworkers. Each latte is $4.75. I'll just type the math straight in:

4 * 4.75

Ruby immediately spits back 19.0. It didn't ask for a filename or a "main" function; it just did the work. Now, let's say there's a 7% sales tax. I'll try to chain that on:

19.0 * 1.07

The result is 20.33. This is why I love irb. If I'm unsure how a specific method works or what a piece of data looks like, I don't write a whole program—I just jump in here and poke it until it tells me the truth.

Breaking things on purpose

Here is where most people get nervous: the error message. Let's intentionally mess up. I'll try to calculate a tip, but I'll forget to close my parentheses:

(20.33 * 0.20

Ruby doesn't give me an answer. Instead, it just gives me a > symbol on the next line. It's telling me, "You started something, but you didn't finish it. I'm still waiting." If I hit enter a few more times or type a closing parenthesis ), it will finally execute. But if I type something completely nonsensical, like this_is_not_a_variable, I'll get a NameError.

Don't fear the red text. In Ruby, the error message usually tells you exactly which line is the culprit and what it expected to find. The trick is to read the last line of the error first—that's usually where the actual problem is described.




📋 Practical Task

Calculating the Monthly Subscription Total

Open your terminal and launch irb. You are tasked with calculating the total cost of a software stack for a small team. Perform the following calculations directly in the REPL:

  • Calculate the cost of 3 licenses for a tool that costs $12.99 per month.
  • Add a one-time setup fee of $50.00 to that total.
  • Apply a 15% discount to the entire sum (multiply the total by 0.85).
  • Try to call a method that doesn't exist (like calculate_magic_numbers) and observe the specific error Ruby returns.

Once you have the final discounted total, type exit to leave the irb session.

Rating
0 0

There are no comments for now.

to be the first to leave a comment.