Skip to Content
Course content

2: Anatomy of a Ruby Script

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

I want to show you how a Ruby script actually fits together. Instead of just giving you a template, let's actually build something and break it a few times. We're going to make a simple ticket price calculator for a movie theater—something that adjusts the price based on age and the time of day.

Just getting it to run

I'll start by creating a file called ticket_calc.rb. I'm going to keep it dead simple. I'll just throw the logic right at the top of the file.

age = 25
time_of_day = "matinee"
price = 15

if time_of_day == "matinee"
  price = 10
end

if age >= 65
  price = 8
end

puts "Your ticket costs $#{price}"

I run this with ruby ticket_calc.rb and it works. But here's the problem: it's a "one-off." I can't reuse this logic anywhere else because it's just floating in the global space. If I wanted to calculate prices for ten different people, I'd have to copy-paste this block ten times. That's a nightmare to maintain.

Wrapping logic into a method

I need to organize this. I'll wrap the logic in a method. Let's see what happens when I do that.

def calculate_ticket_price(age, time_of_day)
  price = 15
  price = 10 if time_of_day == "matinee"
  price = 8 if age >= 65
  price
end

# I'll just leave it at that for a second.

I run ruby ticket_calc.rb again. Nothing. Total silence. Why? Because I've defined the capability to calculate a price, but I haven't actually told Ruby to do it. This is a common point of confusion. Defining a method is like writing a recipe; it doesn't mean the cake is actually baking.

So, I'll add a call to that method at the bottom of the file:

def calculate_ticket_price(age, time_of_day)
  price = 15
  price = 10 if time_of_day == "matinee"
  price = 8 if age >= 65
  price
end

puts "Your ticket costs $#{calculate_ticket_price(25, "matinee")}"

Making it feel like a real tool

Now, I'm tired of typing ruby before the filename every time I run this. If I'm on a Unix-like system (macOS or Linux), I can tell the operating system exactly which interpreter to use. I'll add a "shebang" line to the very top.

#!/usr/bin/env ruby

def calculate_ticket_price(age, time_of_day)
  # ... logic here ...
end

The #!/usr/bin/env ruby line tells the shell, "Hey, don't guess what this is; use the Ruby environment to execute it." Now, if I run chmod +x ticket_calc.rb in my terminal, I can just run ./ticket_calc.rb. It feels like a native command now.

Handling "magic numbers" with constants

Looking at my code, I see 15, 10, and 8. In the industry, we call these "magic numbers." They're dangerous because if the theater raises prices, I have to hunt through my logic to find every instance of 15. I'll move these to constants at the top of the script.

#!/usr/bin/env ruby

STANDARD_PRICE = 15
MATINEE_PRICE = 10
SENIOR_PRICE = 8

def calculate_ticket_price(age, time_of_day)
  price = STANDARD_PRICE
  price = MATINEE_PRICE if time_of_day == "matinee"
  price = SENIOR_PRICE if age >= 65
  price
end

puts "Your ticket costs $#{calculate_ticket_price(70, "evening")}"

Notice I used ALL_CAPS. In Ruby, that's the convention for constants. It tells other developers (and the Ruby interpreter) that this value isn't intended to change while the program is running.

The final structure

So, looking at our finished script, we have a clear anatomy:

  • The Shebang: Tells the OS how to run the file.
  • Constants: Configuration values at the top for easy editing.
  • Method Definitions: The "how-to" logic, isolated and reusable.
  • The Execution Block: The actual calls to those methods that produce output.
This separates the definition of your program from the execution of your program, which is the first step toward writing professional-grade software.


📋 Practical Task

Build a "Gas Mileage Calculator" Script

Create a Ruby script named gas_calc.rb that follows the anatomy we just explored. Your script must include the following:

  • A shebang line at the top to make it executable.
  • A constant for a FUEL_EFFICIENCY_MULTIPLIER (you can pick any number, e.g., 0.85 for "real world" efficiency).
  • A method called calculate_miles that takes gallons and mpg as arguments, multiplies them, and then applies the constant multiplier.
  • A final line that calls the method with sample data and prints the result to the console in a readable sentence.

Once written, try running it using ./gas_calc.rb (after using chmod +x) to ensure your shebang is working correctly.

Rating
0 0

There are no comments for now.

to be the first to leave a comment.