ruby
19 lines · 4 steps
Deduplicating contacts with Enumerable#uniq
How a custom block on uniq lets you collapse duplicate records by a normalized identity rather than exact equality.
Explained by
highlit
1class ContactImporter
2 def initialize(rows)
3 @rows = rows
4 end
5
6 def dedupe
7 @rows.uniq { |row| normalized_email(row) }
8 end
9
10 def dedupe_by_composite
11 @rows.uniq { |row| [normalized_email(row), row[:phone].to_s.gsub(/\D/, "")] }
12 end
13
14 private
15
16 def normalized_email(row)
17 row[:email].to_s.strip.downcase
18 end
19end
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Passing a block to uniq lets you define equality on a derived key instead of the whole object.
- 2Normalizing values before comparing catches duplicates that differ only in casing or formatting.
- 3Returning an array from the uniq block creates a composite key across multiple fields.
Related explainers
ruby
module Instrumentation module Timing def self.[](*method_names) Module.new do
Timing methods with a prepended module in Ruby in Rails
metaprogramming
prepend
instrumentation
Advanced
7 steps
rust
use std::collections::HashSet; use std::io::{self, BufRead, BufWriter, Write}; fn main() -> io::Result<()> {
Filtering duplicate lines in Rust
stdin
hashset
buffering
Beginner
5 steps
ruby
class BulkImporter BATCH_SIZE = 500 def initialize(records)
Batching bulk inserts in Rails
batching
bulk-insert
deduplication
Intermediate
5 steps
ruby
class Credentials SENSITIVE = %i[password api_key secret_token access_key].freeze REDACTED = "[REDACTED]".freeze
Redacting secrets in Ruby object output
serialization
introspection
security
Intermediate
7 steps
typescript
type Fetcher<T> = (key: string) => Promise<T>; export class RequestDeduplicator<T> { private inFlight = new Map<string, Promise<T>>();
Deduplicating in-flight requests in TypeScript
deduplication
promises
caching
Intermediate
7 steps
ruby
module Multitenant extend ActiveSupport::Concern included do
How a multitenant concern scopes Rails models
multitenancy
default-scope
concern
Advanced
8 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/deduplicating-contacts-with-enumerable-uniq-explained-ruby-e87f/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.