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
8: String Methods
A few years ago, I was helping a junior dev debug a "ghost" bug in a user authentication system. He was adamant that the user was typing the correct email, but the database kept returning nil. We spent an hour staring at the code before I noticed the input was coming from a legacy form that didn't trim whitespace. The user had accidentally hit the spacebar after their email address. To a human, "dev@example.com" and "dev@example.com " are the same; to Ruby, they are completely different objects. That's the danger of treating strings as raw text without cleaning them first.
Taming the Chaos of User Input
Most of the time, you aren't just printing strings; you're preparing them for something else—a database query, a URL, or a UI display. This is where methods like .strip, .downcase, and .upcase become your best friends. .strip removes leading and trailing whitespace, which is basically mandatory for any input field you'll ever build. I usually chain these together to ensure data consistency.
raw_email = " User@Example.com "
clean_email = raw_email.strip.downcase
# => "user@example.com"
If you need to change the casing of just the first letter, you can use .capitalize. It's handy for names, though be careful—it forces the first character to uppercase and every other character to lowercase, which might ruin names like "McDonald".
Breaking Apart and Searching Strings
You'll often find yourself with a string that contains multiple pieces of information. Instead of trying to manually calculate indices, use .split. It turns a string into an array based on a delimiter you provide. I use this constantly when parsing CSV data or splitting a full name into a first and last name.
full_name = "Jane Doe"
first, last = full_name.split(" ")
# first => "Jane", last => "Doe"
When you just need to know if a string contains a specific sequence of characters, .include? is the way to go. It returns a boolean, making it perfect for if statements. It's much cleaner than trying to use a complex regular expression for a simple check.
log_message = "ERROR: Database connection failed at 10:00"
if log_message.include?("ERROR")
puts "Alerting the on-call engineer!"
end
The Surgical Precision of Substitution
Sometimes you need to replace a part of a string. While there are a few ways to do this, .gsub (global substitution) is the workhorse. It looks for every occurrence of a pattern and replaces it with something else. I've used this for everything from replacing placeholders in email templates to scrubbing sensitive data from logs.
template = "Hello, [NAME]! Welcome to [CITY]."
personalized = template.gsub("[NAME]", "Alice").gsub("[CITY]", "Berlin")
# => "Hello, Alice! Welcome to Berlin."
One quick tip: remember that strings in Ruby are mutable. Most of these methods return a new string. If you see a method ending in a bang (like .strip!), that's a signal that it modifies the original string in place. I generally avoid bang methods unless I have a very specific memory constraint, as mutating your data can lead to some confusing bugs down the line.
📋 Practical Task
The Messy CSV Name Normalizer
You've been handed a list of user names from a legacy system. The data is a disaster: some names have weird spacing, some are all caps, and some have placeholder characters that need to be removed. Your job is to write a script that cleans these strings into a standardized format: "Firstname Lastname" (where only the first letter of each name is capitalized).
Requirements:
- Start with this array of "dirty" strings:
[" jOHN doe ", "SARAH smith ", " mike jones ", "ALICE_WONDERLAND"] - Remove all leading/trailing whitespace.
- Replace any underscores (
_) with a space. - Ensure the names are formatted consistently (capitalize the first letter, lowercase the rest).
- Print the final cleaned array.
Hint: You'll likely want to use .map to iterate through the array and chain your string methods together.
There are no comments for now.