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
ruby
require "shellwords" require "open3" module Backup
Building safe shell commands in Ruby
shell-out
subprocess
command-injection
Intermediate
7 steps
ruby
class UserAgentParser BROWSERS = [ [/Edg\/([\d.]+)/, "Edge"], [/OPR\/([\d.]+)/, "Opera"],
Parsing user-agent strings in Ruby
regex
pattern-matching
lookup-tables
Intermediate
8 steps
javascript
function evaluate(expression) { const tokens = tokenize(expression); let pos = 0;
Building a recursive descent calculator
parsing
recursion
operator-precedence
Intermediate
8 steps
ruby
class LogAggregator BUCKET_FORMAT = "%Y-%m-%dT%H:%M" def initialize(entries)
Bucketing log entries by the minute in Ruby
aggregation
hashing
enumerable
Intermediate
5 steps
ruby
class WeeklySignupsReport DEFAULT_WEEKS = 12 def initialize(weeks: DEFAULT_WEEKS, source: User.all)
Building a weekly signups report in Rails
service object
aggregation
group by
Intermediate
7 steps
go
package logging import ( "context"
Deduplicating log attributes in Go's slog
decorator-pattern
structured-logging
immutability
Intermediate
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/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.