Lesson 4
Applying Ruby Hashes to Real-World Challenges
Introduction and Goal Setting

Hello there! In this lesson, we will apply Hashes, a data structure, to real-world challenges. Our focus will be on solving tasks such as cataloging books in a library, counting votes in an election, and tracking inventories.

Real-World Scenarios Calling for Hashes

Hashes are beneficial in real-life applications, such as the ones mentioned above, due to their ability to rapidly retrieve data with unique keys and efficiently handle larger datasets. Let's understand their efficiency through some actual examples.

Solving Real-World Task 1: Cataloging Books in a Library

Suppose you're asked to manage the cataloging of books in a library. In Ruby, we represent Hashes using a very similar structure. Here, the book ID serves as the key, while the details of the book, such as the title, author, and year of publication, are stored as values.

This approach allows us to add, search for, and remove books from our library catalog using just a few lines of Ruby code.

Ruby
1library_catalog = {} # Initializing Hash 2 3# Details of a book 4book_id = "123" 5book_details = {title: "To Kill a Mockingbird", author: "Harper Lee", year_published: 1960} 6 7library_catalog[book_id] = book_details # Adding a book to library catalog 8 9if library_catalog.key?(book_id) # Searching for a book 10 puts library_catalog[book_id] 11end 12 13library_catalog.delete(book_id) # Removing a book from library

As you can see, Hashes make the task of cataloging books in the library simpler and more efficient!

Solving Real-World Task 2: Counting Votes in an Election

Imagine a scenario in which we need to count votes in an election. We employ a Hash, where each name is a unique key, and the frequency of that name serves as the associated value. Let's write some Ruby code to better understand this.

Ruby
1votes_list = ["Alice", "Bob", "Alice", "Charlie", "Bob", "Alice"] # Cast votes 2vote_counts = Hash.new(0) # Initializing a Hash with default value 0 3 4# Counting the votes 5votes_list.each do |name| 6 vote_counts[name] += 1 # Increment the count for each vote 7end 8 9puts vote_counts 10# Prints {"Alice"=>3, "Bob"=>2, "Charlie"=>1}

Hashes facilitate the efficient counting of votes.

Solving Real-World Task 3: Tracking Store Inventories

Finally, consider a task that involves managing a store's inventory. Here, we can use a Hash in which product names are keys and quantities are values. This approach allows us to easily add new items, adjust the quantity of items, check whether an item is in stock, and much more.

Ruby
1store_inventory = {"Apples" => 100, "Bananas" => 80, "Oranges" => 90} # Initializing an inventory 2 3store_inventory["Pears"] = 50 # Adding a new product to inventory and setting its quantity 4 5store_inventory["Apples"] += 20 # Updating number of apples in inventory 6 7prod = "Apples" # A product to be checked 8puts "Total #{prod} in stock: #{store_inventory[prod]}" 9 10# Check if a product is in stock 11prod = "Mangoes" 12if store_inventory.key?(prod) # If mangoes exist in inventory 13 puts "#{prod} are in stock." 14else # If mangoes doesn't exist in inventory 15 puts "#{prod} are out of stock." 16end

Thus, when managing inventory data, Hashes offer an efficient solution!

Lesson Summary and Practice

In this lesson, we bridged the gap between the theory of Hashes and their practical applications. We explored real-world problems that can be solved using Hashes and implemented Ruby code to address them.

Now get ready for hands-on practice exercises that will help reinforce these concepts and hone your Hash problem-solving skills. Happy coding!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.