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
248: Building a Social Bookmarking Service
By now, you've got a handle on the core syntax and object-oriented patterns of Ruby. Today, we're going to put that into practice by building the engine for a social bookmarking service. We aren't worrying about the database or the web frontend right now—we're focusing on the domain logic: how we store, tag, and retrieve links efficiently.
Designing the Bookmark Object
I like to start by defining a simple data structure. A bookmark isn't just a URL; it needs a title and a set of tags. I'll use a Struct here because it's a clean way to create a class that primarily holds data without needing a bunch of boilerplate attr_accessor calls.
Bookmark = Struct.new(:id, :url, :title, :tags)
Now we need a manager class to handle the collection. I'll call it BookmarkService. This is where the "social" part begins—even though we're starting with a single-user perspective, we want the architecture to support scaling to many users later.
Managing the Collection
In my first pass, I'll create a class that holds an array of bookmarks. I want to be able to add a bookmark and then find all bookmarks associated with a specific tag. Here's how I'd initially sketch that out:
class BookmarkService
def initialize
@bookmarks = []
@next_id = 1
end
def add_bookmark(url, title, tags = [])
bookmark = Bookmark.new(@next_id, url, title, tags)
@bookmarks < < bookmark
@next_id += 1
bookmark
end
def find_by_tag(tag)
@bookmarks.select { |b| b.tags.include?(tag) }
end
end
The "Wait, this is slow" Moment
I've made a classic mistake here. I'm using select to filter bookmarks by tag. For ten bookmarks, it's fine. For ten thousand? We're iterating through the entire list every single time someone wants to see their "Ruby" tags. That's an O(n) operation, and in a real-world service, that's a performance killer.
I need an index. Instead of just a list, I'll maintain a hash where the keys are tags and the values are arrays of bookmarks. I'll rewrite the add_bookmark method to update this index on the fly. It adds a bit of complexity to the write operation, but it makes the read operation nearly instantaneous.
class BookmarkService
def initialize
@bookmarks = []
@tag_index = Hash.new { |hash, key| hash[key] = [] }
@next_id = 1
end
def add_bookmark(url, title, tags = [])
bookmark = Bookmark.new(@next_id, url, title, tags)
@bookmarks < < bookmark
# Update the index for each tag
tags.each { |tag| @tag_index[tag] < < bookmark }
@next_id += 1
bookmark
end
def find_by_tag(tag)
@tag_index[tag]
end
end
Adding Search and Refinement
To make this actually useful, we need more than just tag lookups. I want to be able to search titles for keywords. I'll add a search method using a regular expression. I'm opting for a case-insensitive search because users rarely remember exactly how they capitalized a title six months ago.
class BookmarkService # ... previous methods ... def search(query) regex = Regexp.new(query, Regexp::IGNORECASE) @bookmarks.select { |b| b.title =~ regex } end endNow we have a functioning core. We can add links, categorize them via an index for speed, and search through them using regex. It's a lean implementation, but it follows the primary rule of software engineering: solve the immediate problem, but don't build a system that collapses the moment your data grows.
📋 Practical Task
Implementing a Duplicate URL Guard
Right now, the BookmarkService allows a user to add the same URL multiple times, which would clutter any real bookmarking app. Your task is to modify the BookmarkService class to prevent duplicate URLs.
- Modify the
add_bookmarkmethod so that it checks if the URL already exists in the@bookmarkscollection. - If the URL exists, the method should return
nil(or raise a custom error) instead of adding the bookmark and incrementing the ID. - Ensure that the
@tag_indexis not updated if the bookmark is a duplicate.
Test your implementation by attempting to add the same URL twice and verifying that the second call does not increase the total count of bookmarks.
There are no comments for now.