0
0
Rubyprogramming~30 mins

Method chaining patterns in Ruby - Mini Project: Build & Apply

Choose your learning style9 modes available
Method chaining patterns
📖 Scenario: Imagine you are working with a list of book titles in a library system. You want to process these titles by cleaning up the text and then selecting only the titles that meet certain conditions.
🎯 Goal: You will build a Ruby program that uses method chaining to clean and filter a list of book titles step-by-step.
📋 What You'll Learn
Create an array of book titles with exact values
Create a variable for the minimum length of titles to keep
Use method chaining to clean and filter the titles
Print the final filtered list of titles
💡 Why This Matters
🌍 Real World
Cleaning and filtering lists of text data is common in apps like library catalogs, contact lists, or product inventories.
💼 Career
Understanding method chaining helps you write clean, readable code for data processing tasks in Ruby development jobs.
Progress0 / 4 steps
1
Create the initial array of book titles
Create an array called book_titles with these exact strings: " The Hobbit ", "War and Peace", "1984", " Brave New World ", and "Ulysses".
Ruby
Need a hint?

Use square brackets [] to create an array and include the exact strings with quotes.

2
Set the minimum title length
Create a variable called min_length and set it to 10.
Ruby
Need a hint?

Just assign the number 10 to the variable min_length.

3
Use method chaining to clean and filter titles
Create a new variable called filtered_titles that uses method chaining on book_titles to first remove extra spaces from each title using map(&:strip), then select only titles with length greater than or equal to min_length using select.
Ruby
Need a hint?

Use map(&:strip) to remove spaces, then select with a block to keep titles with length >= min_length.

4
Print the filtered titles
Write a puts statement to print the filtered_titles array.
Ruby
Need a hint?

Use puts filtered_titles to print each title on its own line.