Ruby
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Methods and Blocks
-
Section 4: Object-Oriented Ruby
-
Section 5: Metaprogramming
-
Section 6: Working with Data and Files
-
Section 7: Ruby Frameworks Overview
-
Section 8: Ecosystem and Testing
-
Section 9: Practical Projects
-
Section 10: Interview Practice
-
Section 11: Data Structures and Algorithms in Ruby
-
Section 12: More Practice Exercises
-
Section 13: Enumerable and Functional Style
-
Section 14: More OOP Practice
-
Section 15: More Testing
-
Section 16: Enumerable and Comparable Modules In Depth
-
Section 17: Ruby Standard Library: Core Utilities
-
Section 18: Ruby Standard Library: Data and Security
-
Section 19: Ruby Standard Library: CLI and Text
-
Section 20: Ruby Networking
-
Section 21: Ruby on Rails Deep Dive
-
Section 22: Ruby Metaprogramming Deep Dive
-
Section 23: Ruby Design Patterns
-
Section 24: Ruby Concurrency
-
Section 25: Ruby Testing Deep Dive
-
Section 26: Ruby Gems and Packaging
-
Section 27: Ruby Performance
-
Section 28: More Practice Exercises
-
Section 29: More Interview Practice
-
Section 30: Rails API Development
-
Section 31: Rails Authentication and Authorization
-
Section 32: Rails Testing Deep Dive
-
Section 33: Rails Performance
-
Section 34: Rails Deployment
-
Section 35: Sinatra and Lightweight Ruby Web Apps
-
Section 36: More Ruby Language Deep Dive
-
Section 37: Ruby 3.x Modern Features
-
Section 38: More Data Structures in Ruby
-
Section 39: More Practical Projects
-
Section 40: Ruby Ecosystem Tools
-
Section 41: More Practice and Review
-
Section 42: Final Practice and Mastery
-
Section 43: Ruby Interview Deep Dive
-
Section 44: Ruby Background Processing Deep Dive
-
Section 45: Ruby GraphQL
-
Section 46: Ruby Object Model Deep Dive
-
Section 47: Ruby Hanami Framework Overview
-
Section 48: Ruby gRPC and Protocol Buffers
-
Section 49: Ruby Data Processing
-
Section 50: Ruby Search Integration
-
Section 51: Ruby File Upload and Media
-
Section 52: Ruby Email and Notifications
-
Section 53: Ruby Admin Panels
-
Section 54: Ruby Feature Flags and Experimentation
-
Section 55: Ruby Monitoring and Observability
-
Section 56: Ruby Docker and Deployment Deep Dive
-
Section 57: Ruby Security Deep Dive
-
Section 58: More Advanced Metaprogramming
-
Section 59: More Final Projects
2: Anatomy of a Ruby Script
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.
📋 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_milesthat takesgallonsandmpgas 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.
There are no comments for now.