ruby
28 lines · 8 steps
Recursively merging nested config hashes in Ruby
A module that deep-merges two hashes, combining nested hashes and arrays instead of clobbering them.
Explained by
highlit
1module ConfigMerge
2 module_function
3
4 def deep_merge(base, override)
5 base.merge(override) do |_key, base_val, override_val|
6 if base_val.is_a?(Hash) && override_val.is_a?(Hash)
7 deep_merge(base_val, override_val)
8 elsif base_val.is_a?(Array) && override_val.is_a?(Array)
9 (base_val + override_val).uniq
10 else
11 override_val
12 end
13 end
14 end
15
16 def deep_merge!(base, override)
17 override.each do |key, override_val|
18 base_val = base[key]
19 base[key] =
20 if base_val.is_a?(Hash) && override_val.is_a?(Hash)
21 deep_merge!(base_val, override_val)
22 else
23 override_val
24 end
25 end
26 base
27 end
28end
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Passing a block to `merge` lets you decide per-key how conflicting values combine instead of blindly overwriting.
- 2Recursing into nested hashes lets a merge preserve deep structure rather than replacing whole subtrees.
- 3Offering both a pure and a bang variant gives callers the choice between safety and in-place efficiency.
Related explainers
ruby
module DeepFreeze module_function def call(obj)
Recursively freezing nested Ruby data
recursion
immutability
pattern matching
Intermediate
9 steps
typescript
import { Module, Injectable } from '@nestjs/common'; import { ConfigModule, ConfigService } from '@nestjs/config'; import * as Joi from 'joi';
Validated, typed config in NestJS
configuration
validation
dependency-injection
Intermediate
6 steps
ruby
class Webhooks::StripeController < ApplicationController skip_before_action :verify_authenticity_token skip_before_action :authenticate_user!
How a Rails Stripe webhook controller works
webhooks
signature verification
event dispatch
Intermediate
7 steps
java
public record ServerConfig(String host, int port, boolean tls, List<String> allowedOrigins) { public ServerConfig { Objects.requireNonNull(host, "host is required");
Validating config with a Java record
records
validation
immutability
Intermediate
9 steps
python
from functools import lru_cache import math
Memoizing number theory with lru_cache
memoization
number-theory
caching
Intermediate
8 steps
ruby
class ReportGenerator def initialize(account, period) @account = account @period = period
Memoized report metrics in Ruby in Rails
memoization
query-composition
service-object
Intermediate
7 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/recursively-merging-nested-config-hashes-in-ruby-explained-ruby-d7fc/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.