ruby
46 lines · 8 steps
Deep-freezing config with recursive immutability
A recursive freeze-and-copy pair backs an immutable Config whose updates return new instances.
Explained by
highlit
1module DeepImmutable
2 module_function
3
4 def deep_freeze(obj)
5 case obj
6 when Hash
7 obj.each_value { |v| deep_freeze(v) }
8 when Array
9 obj.each { |v| deep_freeze(v) }
10 end
11 obj.freeze
12 end
13
14 def deep_dup(obj)
15 case obj
16 when Hash
17 obj.each_with_object({}) { |(k, v), acc| acc[k] = deep_dup(v) }
18 when Array
19 obj.map { |v| deep_dup(v) }
20 when Numeric, Symbol, NilClass, TrueClass, FalseClass
21 obj
22 else
23 obj.dup
24 end
25 end
26end
27
28class Config
29 def initialize(settings)
30 @settings = DeepImmutable.deep_freeze(DeepImmutable.deep_dup(settings))
31 end
32
33 def [](key)
34 @settings[key]
35 end
36
37 def with(overrides)
38 merged = DeepImmutable.deep_dup(@settings)
39 overrides.each { |k, v| merged[k] = v }
40 Config.new(merged)
41 end
42
43 def to_h
44 DeepImmutable.deep_dup(@settings)
45 end
46end
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Recursion is the natural fit for making nested structures fully immutable or fully copied.
- 2Freezing a duplicated input protects internal state from outside mutation of the original object.
- 3Returning a new instance from update methods keeps a value object genuinely immutable.
Related explainers
java
package com.example.time; import java.time.Duration; import java.time.Instant;
Adding ISO-8601 durations across time zones in Java
date-time
iso-8601
parsing
Intermediate
7 steps
ruby
class Admin::OrdersExportController < Admin::BaseController include ActionController::Live def show
Streaming a CSV export in Rails
streaming
csv-export
batching
Advanced
8 steps
ruby
class DatasetImportJob < ApplicationJob queue_as :default def perform(import)
Streaming import progress with a Rails job
background-jobs
turbo-streams
progress-tracking
Intermediate
9 steps
ruby
class Booking < ApplicationRecord belongs_to :room belongs_to :guest
Guarding booking dates in a Rails model
validations
associations
scopes
Intermediate
5 steps
java
public final class HttpHeaders { private final NavigableMap<String, List<String>> values = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
A case-insensitive HTTP headers map in Java
case-insensitivity
multimap
immutability
Intermediate
7 steps
ruby
class DashboardMetrics CACHE_TTL = 15.minutes def initialize(account)
Caching dashboard metrics in Rails
caching
service-object
query-optimization
Intermediate
5 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/deep-freezing-config-with-recursive-immutability-explained-ruby-fb53/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.