📖 Scenario: You work at a bookstore that wants to organize books by their genre to make it easier for customers to find what they like.
🎯 Goal: You will create a Ruby program that groups a list of books by their genre using the group_by method.
📋 What You'll Learn
Create an array of hashes called books with each book having :title and :genre keys
Create a variable called grouped_books to store the result of grouping books by genre using group_by
Print the grouped_books variable to show books categorized by genre
💡 Why This Matters
🌍 Real World
Grouping items by category is common in apps like bookstores, music libraries, or inventory systems to help users find things easily.
💼 Career
Understanding how to group data efficiently is important for software developers working with collections, databases, or data analysis.
Progress0 / 4 steps
1
Create the books array
Create an array called books with these exact hashes: { title: 'The Hobbit', genre: 'Fantasy' }, { title: '1984', genre: 'Dystopian' }, { title: 'The Catcher in the Rye', genre: 'Classic' }, { title: 'The Lord of the Rings', genre: 'Fantasy' }, { title: 'Brave New World', genre: 'Dystopian' }
Ruby
Hint
Use square brackets [] to create an array and curly braces {} for each book hash.
2
Prepare to group books by genre
Create a variable called grouped_books and set it to nil for now as a placeholder.
Ruby
Hint
This step sets up a variable to hold the grouped result later.
3
Group the books by genre
Use the group_by method on the books array to group books by their :genre key. Assign the result to the variable grouped_books.
Ruby
Hint
Use group_by { |book| book[:genre] } to group books by genre.
4
Print the grouped books
Write a puts statement to print the grouped_books variable so you can see the books grouped by genre.
Ruby
Hint
Use puts grouped_books to display the grouped result.
Practice
(1/5)
1. What does the Ruby method group_by do when called on an array?
easy
A. It converts the array into a string.
B. It groups elements into a hash based on a given condition.
C. It removes duplicate elements from the array.
D. It sorts the array in ascending order.
Solution
Step 1: Understand the purpose of group_by
The group_by method organizes elements by a condition and returns a hash.
Step 2: Compare with other array methods
Sorting, removing duplicates, or converting to string are different methods like sort, uniq, or join.
Final Answer:
It groups elements into a hash based on a given condition. -> Option B
Quick Check:
group_by = groups hash [OK]
Hint: Remember: group_by returns a hash of grouped elements [OK]
Common Mistakes:
Confusing group_by with sort or uniq
Expecting an array instead of a hash
Thinking it changes element order only
2. Which of the following is the correct syntax to group an array nums by even or odd using group_by?
easy
A. nums.group_by(n % 2 == 0)
B. nums.group_by do n % 2 == 0 end
C. nums.group_by { |n| n % 2 == 0 }
D. nums.group_by ->(n) { n % 2 == 0 }
Solution
Step 1: Recall block syntax for group_by
The method requires a block with curly braces or do-end and a block variable.
Step 2: Check each option's syntax
nums.group_by { |n| n % 2 == 0 } uses correct block with { |n| ... }. nums.group_by(n % 2 == 0) misses block. nums.group_by do n % 2 == 0 end lacks block variable. nums.group_by ->(n) { n % 2 == 0 } uses lambda incorrectly.
Which code correctly groups people by their age using group_by?
hard
A. people.group_by { |person| person[:age] }
B. people.group_by { |person| person.name }
C. people.group_by { |person| person['age'] }
D. people.group_by { |person| person.age }
Solution
Step 1: Understand data structure and access
Each element is a hash with symbol keys like :age, so access with person[:age].
Step 2: Check each option's key access
people.group_by { |person| person[:age] } uses correct symbol key access. people.group_by { |person| person.name } and D use dot notation which doesn't work on hashes. people.group_by { |person| person['age'] } uses string key which is incorrect here.
Final Answer:
people.group_by { |person| person[:age] } -> Option A