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

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, and published_year.
  • Add a fourth book to the library array using the shovel (<<) operator.
  • Write a loop that iterates through the library and prints a sentence for each book in this format: "The Great Gatsby was written by F. Scott Fitzgerald in 1925."
  • Finally, update the published_year of the second book in your list to a different year to practice modifying hash values within an array.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.