Ruby
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Methods and Blocks
-
Section 4: Object-Oriented Ruby
-
Section 5: Metaprogramming
-
Section 6: Working with Data and Files
-
Section 7: Ruby Frameworks Overview
-
Section 8: Ecosystem and Testing
-
Section 9: Practical Projects
-
Section 10: Interview Practice
-
Section 11: Data Structures and Algorithms in Ruby
-
Section 12: More Practice Exercises
-
Section 13: Enumerable and Functional Style
-
Section 14: More OOP Practice
-
Section 15: More Testing
-
Section 16: Enumerable and Comparable Modules In Depth
-
Section 17: Ruby Standard Library: Core Utilities
-
Section 18: Ruby Standard Library: Data and Security
-
Section 19: Ruby Standard Library: CLI and Text
-
Section 20: Ruby Networking
-
Section 21: Ruby on Rails Deep Dive
-
Section 22: Ruby Metaprogramming Deep Dive
-
Section 23: Ruby Design Patterns
-
Section 24: Ruby Concurrency
-
Section 25: Ruby Testing Deep Dive
-
Section 26: Ruby Gems and Packaging
-
Section 27: Ruby Performance
-
Section 28: More Practice Exercises
-
Section 29: More Interview Practice
-
Section 30: Rails API Development
-
Section 31: Rails Authentication and Authorization
-
Section 32: Rails Testing Deep Dive
-
Section 33: Rails Performance
-
Section 34: Rails Deployment
-
Section 35: Sinatra and Lightweight Ruby Web Apps
-
Section 36: More Ruby Language Deep Dive
-
Section 37: Ruby 3.x Modern Features
-
Section 38: More Data Structures in Ruby
-
Section 39: More Practical Projects
-
Section 40: Ruby Ecosystem Tools
-
Section 41: More Practice and Review
-
Section 42: Final Practice and Mastery
-
Section 43: Ruby Interview Deep Dive
-
Section 44: Ruby Background Processing Deep Dive
-
Section 45: Ruby GraphQL
-
Section 46: Ruby Object Model Deep Dive
-
Section 47: Ruby Hanami Framework Overview
-
Section 48: Ruby gRPC and Protocol Buffers
-
Section 49: Ruby Data Processing
-
Section 50: Ruby Search Integration
-
Section 51: Ruby File Upload and Media
-
Section 52: Ruby Email and Notifications
-
Section 53: Ruby Admin Panels
-
Section 54: Ruby Feature Flags and Experimentation
-
Section 55: Ruby Monitoring and Observability
-
Section 56: Ruby Docker and Deployment Deep Dive
-
Section 57: Ruby Security Deep Dive
-
Section 58: More Advanced Metaprogramming
-
Section 59: More Final Projects
209: Building a Faceted Search Feature
Faceted search sounds like a high-end enterprise feature, but at its core, it's just a way to let users narrow down a large dataset by applying multiple, optional filters. Think of any e-commerce site where you check "Blue," "Size Large," and "Under $50" simultaneously. In Ruby, the trick is to build a flexible system that doesn't break when a user leaves some filters blank.
To keep this concrete, let's build a search feature for a board game collection. We'll want to filter by genre, player count, and complexity.
Setting up our board game data
Before we get to the logic, we need something to search. I'll use a simple array of hashes here. In a real app, this would be an ActiveRecord query, but the logic for handling the facets remains the same.
GAMES = [
{ title: "Terraforming Mars", genre: "Sci-Fi", players: 5, complexity: "High" },
{ title: "Catan", genre: "Strategy", players: 4, complexity: "Medium" },
{ title: "Ticket to Ride", genre: "Strategy", players: 5, complexity: "Low" },
{ title: "Pandemic", genre: "Co-op", players: 4, complexity: "Medium" },
{ title: "Azul", genre: "Abstract", players: 4, complexity: "Low" },
{ title: "Spirit Island", genre: "Co-op", players: 4, complexity: "High" }
]
The first (flawed) attempt at filtering
My first instinct is usually to write a method that takes each facet as an argument and chains a bunch of select calls. Let's see how that looks.
class GameSearch
def self.search(genre: nil, complexity: nil)
results = GAMES.select { |g| g[:genre] == genre }
results = results.select { |g| g[:complexity] == complexity }
results
end
end
I'll be honest: I've written this exact pattern a dozen times and then immediately regretted it. Here is the problem: if the user doesn't select a genre (meaning genre is nil), the first select will look for games where the genre is explicitly nil. Since all our games have genres, it returns an empty array immediately. The search "breaks" unless the user fills out every single filter.
Making the filters optional
To fix this, we need to tell Ruby: "Only apply this filter if the user actually provided a value." I prefer using the all? method on the criteria themselves. It's much cleaner than writing a dozen if statements.
I'm going to move the filters into a hash. This makes the code more scalable; if we add a "Year" or "Designer" facet later, we don't have to keep adding arguments to our method.
class GameSearch
def self.search(filters = {})
GAMES.select do |game|
# We check every filter provided by the user
filters.all? do |key, value|
# If the user didn't provide a value for this facet, ignore it (return true)
# Otherwise, check if the game's attribute matches the value
value.nil? || value.empty? || game[key.to_sym] == value
end
end
end
end
Now, if I call GameSearch.search(complexity: "High"), the genre filter isn't in the hash, so all? only evaluates the complexity. We get "Terraforming Mars" and "Spirit Island" without the search crashing or returning nothing.
Handling multi-select facets
In the real world, users rarely want just one genre. They want "Sci-Fi OR Strategy." To handle this, we should allow the filter values to be arrays. I'll tweak the comparison logic to use include?.
class GameSearch
def self.search(filters = {})
GAMES.select do |game|
filters.all? do |key, value|
next true if value.nil? || (value.respond_to?(:empty?) && value.empty?)
# Cast the game value to an array so we can use .include?
# regardless of whether the filter is a single string or a list
game_value = game[key.to_sym]
Array(value).include?(game_value)
end
end
end
end
# Now we can do this:
# GameSearch.search(genre: ["Sci-Fi", "Strategy"])
By wrapping value in Array(), I'm normalizing the input. If the user passes a single string "Sci-Fi", it becomes ["Sci-Fi"]. If they pass an array, it stays an array. This is a handy Ruby trick that saves me from writing a lot of type-checking boilerplate.
📋 Practical Task
Build a Multi-Facet Book Catalog Filter
You are building a search feature for a digital bookstore. Create a BookSearch class and a BOOKS constant containing at least 5 books. Each book should have a title, genre (e.g., "Fantasy", "Mystery"), and format (e.g., "Hardcover", "E-book").
Implement a .search(filters = {}) method that allows users to filter by one or both of these facets. Your implementation must satisfy these three requirements:
- If the
filtershash is empty, it should return all books. - If a filter is provided as an array (e.g.,
genre: ["Fantasy", "Mystery"]), it should return books matching any of those genres. - The search should not fail or return an empty list if only one of the facets is provided.
Test your code by searching for all "E-books" regardless of genre, and then searching for books that are either "Fantasy" or "Mystery" and are "Hardcover".
There are no comments for now.