ruby
40 lines · 7 steps
Building an EventEmitter in Ruby
A pub/sub class that registers blocks per event and fires them on emit, including one-shot listeners.
Explained by
highlit
1class EventEmitter
2 def initialize
3 @listeners = Hash.new { |hash, key| hash[key] = [] }
4 end
5
6 def on(event, &block)
7 raise ArgumentError, "a block is required" unless block_given?
8
9 @listeners[event] << block
10 block
11 end
12
13 def once(event, &block)
14 wrapper = nil
15 wrapper = lambda do |*args|
16 off(event, wrapper)
17 block.call(*args)
18 end
19 @listeners[event] << wrapper
20 wrapper
21 end
22
23 def off(event, handler)
24 @listeners[event].delete(handler)
25 @listeners.delete(event) if @listeners[event].empty?
26 end
27
28 def emit(event, *args)
29 return false unless @listeners.key?(event)
30
31 @listeners[event].dup.each do |listener|
32 listener.call(*args)
33 end
34 true
35 end
36
37 def listeners(event)
38 @listeners[event].dup
39 end
40end
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1A Hash with a default block lets you append to per-key collections without checking existence first.
- 2Capturing a lambda in a variable it references lets the callback unregister itself when it runs.
- 3Duplicating a collection before iterating protects you from listeners that mutate it mid-loop.
Related explainers
ruby
require "csv" class SalesReport def initialize(path)
Aggregating CSV sales data in Ruby
data-aggregation
memoization
group_by
Intermediate
6 steps
ruby
module DurationFormatter UNITS = [ ['week', 604_800], ['day', 86_400],
Turning seconds into human-readable durations in Ruby
greedy-decomposition
modular-arithmetic
formatting
Intermediate
7 steps
ruby
class Comment < ApplicationRecord belongs_to :post belongs_to :author, class_name: "User"
Live-updating comments with Turbo in Rails
turbo-streams
callbacks
associations
Intermediate
8 steps
javascript
const RATE_LIMIT = 100; const WINDOW_MS = 60 * 1000; const BLOCK_MS = 5 * 60 * 1000;
Building a rate-limiting middleware in Express
rate-limiting
middleware
closures
Intermediate
9 steps
ruby
require 'json' require 'set' class SensitiveScrubber
Recursively scrubbing secrets from JSON
recursion
data-masking
pattern-matching
Intermediate
7 steps
rust
use std::collections::HashMap; pub struct Memoizer<K, V, F> { cache: HashMap<K, V>,
A generic memoizer in Rust
memoization
generics
caching
Intermediate
6 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/building-an-eventemitter-in-ruby-explained-ruby-1f53/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.