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
6: Arrays and Hashes
How do I actually store a list of things and get them back out?
When you have a collection of similar items—like a list of usernames or a series of temperature readings—you want an Array. Think of it as a numbered list. In Ruby, we use square brackets [] to define them. One thing I always remind people: Ruby arrays are zero-indexed, meaning the first item is at position 0, not 1. It feels weird at first, but it's the industry standard.
# Let's imagine we're building a simple shopping list
shopping_list = ["Organic Eggs", "Almond Milk", "Sourdough Bread"]
# Getting the first item
puts shopping_list[0] # => Organic Eggs
# Adding something to the end of the list
shopping_list << "Avocados"
# Removing the last item (maybe we decided we don't need avocados)
shopping_list.pop
You'll find yourself using the << (shovel) operator constantly. It's much more common in Ruby than calling a .push method, though both do the same thing. Just keep in mind that arrays are ordered; if the sequence of your data matters, this is your tool.
What's the deal with these symbols in Hashes? Why not just use strings?
Hashes are where Ruby gets really powerful. Instead of a numbered list, you have key-value pairs. You'll see people using symbols—those words starting with a colon, like :name—as keys. I've had students ask why we don't just use strings like "name".
Here's the secret: strings are mutable, but symbols are immutable. Every time you create the string "name", Ruby creates a new object in memory. But :name is always the exact same object. This makes hashes with symbol keys faster and more memory-efficient. Plus, the syntax is cleaner.
# A hash representing a user profile
user = {
username: "ruby_dev_99",
email: "dev@example.com",
join_date: "2023-05-12"
}
# Accessing a value via its key
puts user[:username] # => ruby_dev_99
# Adding a new key-value pair
user[:status] = "Active"
How do I handle a list of complex objects, like a database result?
In the real world, you're rarely dealing with just a simple list of strings or a single hash. Usually, you're dealing with an Array of Hashes. This is essentially how almost every API returns data. If you're building a product catalog, you don't want one giant hash; you want a list where each item is its own hash.
# A collection of products in a store
inventory = [
{ id: 1, name: "Mechanical Keyboard", price: 120, stock: 5 },
{ id: 2, name: "Gaming Mouse", price: 60, stock: 12 },
{ id: 3, name: "UltraWide Monitor", price: 450, stock: 2 }
]
# Now we can iterate through the array and access specific hash keys
inventory.each do |product|
puts "#{product[:name]} costs $#{product[:price]}"
end
When you combine these two structures, you can model almost any data set. I personally find that mapping over an array of hashes is the most frequent operation I perform in a typical Ruby on Rails controller. Get comfortable with this pattern, and you'll be ahead of the curve.
📋 Practical Task
Building a Digital Library Catalog
Your task is to create a small system to manage a collection of books. Follow these requirements:
- Create an array called
library. - Inside that array, add three hashes. Each hash should represent a book with the keys:
title,author, andpublished_year. - Add a fourth book to the
libraryarray using the shovel (<<) operator. - Write a loop that iterates through the
libraryand prints a sentence for each book in this format: "The Great Gatsby was written by F. Scott Fitzgerald in 1925." - Finally, update the
published_yearof the second book in your list to a different year to practice modifying hash values within an array.
There are no comments for now.