📖 Scenario: You are organizing a small library's book collection. The books are grouped by genre, and you want to learn how to use Ruby array methods to explore this collection.
🎯 Goal: Build a Ruby program that uses array methods length, include?, and flatten to find out how many genres there are, check if a specific book is in the collection, and combine all books into one list.
📋 What You'll Learn
Create an array of arrays called library with exact genres and books
Create a variable called genre_count to store the number of genres
Use include? method to check if the book 'The Hobbit' is in the library
Use flatten method to combine all books into one array called all_books
Print the results exactly as instructed
💡 Why This Matters
🌍 Real World
Libraries, bookstores, and personal collections often organize items in groups. Knowing how to count groups, check for items, and combine lists helps manage collections easily.
💼 Career
Understanding array methods is essential for data handling in programming jobs, especially when working with lists, databases, or APIs that return nested data.
Progress0 / 4 steps
1
Create the library array
Create an array called library with these exact genres and books: [['Fantasy', 'The Hobbit', 'Harry Potter'], ['Science Fiction', 'Dune', 'Neuromancer'], ['Mystery', 'Sherlock Holmes', 'Gone Girl']]
Ruby
Hint
Remember to use square brackets [] to create arrays and separate items with commas.
2
Count the number of genres
Create a variable called genre_count and set it to the length of the library array using the length method.
Ruby
Hint
Use array.length to find how many items are in an array.
3
Check if 'The Hobbit' is in the library
Create a variable called has_hobbit and set it to true or false by checking if the string 'The Hobbit' is included anywhere in the library array using the include? method on a flattened version of library.
Ruby
Hint
Use flatten to make one array from nested arrays, then use include? to check for an item.
4
Flatten the library and print results
Create a variable called all_books that is the flattened version of library. Then print genre_count, has_hobbit, and all_books each on its own line using puts.
Ruby
Hint
Use puts to print each value. Use inspect to print the array as a string.
Practice
(1/5)
1. What does the length method do when called on an array in Ruby?
easy
A. Returns the number of elements in the array
B. Checks if an element exists in the array
C. Flattens nested arrays into a single array
D. Removes duplicate elements from the array
Solution
Step 1: Understand the purpose of length
The length method counts how many items are inside an array.
Step 2: Compare with other methods
include? checks for presence, flatten merges nested arrays, so they do different things.
Final Answer:
Returns the number of elements in the array -> Option A
Quick Check:
length = count items [OK]
Hint: Remember length counts items, not checks or changes arrays [OK]
Common Mistakes:
Confusing length with include? method
Thinking length flattens nested arrays
Assuming length removes duplicates
2. Which of the following is the correct syntax to check if the array arr includes the number 5?
easy
A. arr.include(5)
B. arr.includes?(5)
C. arr.has?(5)
D. arr.include?(5)
Solution
Step 1: Recall the correct method name and syntax
The method to check presence is include? with a question mark at the end.
Step 2: Verify method call on array
It should be called as arr.include?(5), not include or includes?.
Final Answer:
arr.include?(5) -> Option D
Quick Check:
Correct method is include? with question mark [OK]
Hint: Remember Ruby methods with ? end with a question mark [OK]
Common Mistakes:
Omitting the question mark in include?
Using incorrect method names like includes? or has?