ruby
55 lines · 8 steps
A typed config loader in Ruby
How AppConfig loads YAML, selects the current environment, and reads nested keys with typed accessors.
Explained by
highlit
1require "yaml"
2require "pathname"
3
4class AppConfig
5 class MissingKeyError < KeyError; end
6
7 def self.load(path, env: ENV.fetch("RACK_ENV", "development"))
8 raw = YAML.safe_load(Pathname(path).read, aliases: true) || {}
9 new(raw.fetch(env, raw))
10 end
11
12 def initialize(data)
13 @data = deep_symbolize(data)
14 end
15
16 def fetch(*keys)
17 keys.reduce(@data) do |node, key|
18 node.fetch(key)
19 rescue KeyError, NoMethodError
20 raise MissingKeyError, "missing config key: #{keys.join(".")}"
21 end
22 end
23
24 def string(*keys)
25 fetch(*keys).to_s
26 end
27
28 def integer(*keys)
29 Integer(fetch(*keys))
30 end
31
32 def boolean(*keys)
33 value = fetch(*keys)
34 return value if [true, false].include?(value)
35
36 %w[true 1 yes on].include?(value.to_s.strip.downcase)
37 end
38
39 def list(*keys)
40 Array(fetch(*keys))
41 end
42
43 private
44
45 def deep_symbolize(value)
46 case value
47 when Hash
48 value.each_with_object({}) { |(k, v), acc| acc[k.to_sym] = deep_symbolize(v) }
49 when Array
50 value.map { |item| deep_symbolize(item) }
51 else
52 value
53 end
54 end
55end
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1A custom error subclass lets callers distinguish a missing config key from any other failure.
- 2Symbolizing keys recursively gives the whole config tree one consistent access convention.
- 3Typed accessors that wrap a single fetch keep coercion logic in one place instead of at every call site.
Related explainers
rust
use serde::Deserialize; #[derive(Debug, Deserialize)] #[serde(untagged)]
Parsing flexible JSON shapes with serde
deserialization
enums
json
Intermediate
6 steps
ruby
require "shellwords" require "open3" module Backup
Building safe shell commands in Ruby
shell-out
subprocess
command-injection
Intermediate
7 steps
typescript
import { Module } from '@nestjs/common'; import { ConfigModule } from '@nestjs/config'; import * as Joi from 'joi';
Validating env config at boot in NestJS
configuration
schema-validation
environment-variables
Intermediate
8 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
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/a-typed-config-loader-in-ruby-explained-ruby-f377/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.