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
ruby
class Order class InvalidTransition < StandardError; end TRANSITIONS = {
A state machine for order transitions in Ruby
state-machine
data-driven
error-handling
Intermediate
8 steps
ruby
class CreateOrderItems < ActiveRecord::Migration[7.1] def change create_table :order_items do |t| t.references :order, null: false, foreign_key: { on_delete: :cascade }
Enforcing order-item integrity in Rails
migrations
foreign-keys
validations
Intermediate
7 steps
rust
use std::cmp::Ordering; use std::str::FromStr; #[derive(Debug, Clone, PartialEq, Eq)]
Parsing and ordering semantic versions in Rust
parsing
trait-implementation
ordering
Intermediate
8 steps
javascript
'use client'; import { useEffect } from 'react'; import * as Sentry from '@sentry/nextjs';
How a Next.js error boundary recovers
error-boundary
error-handling
observability
Intermediate
8 steps
ruby
class Registration < ApplicationRecord belongs_to :event validates :email, presence: true, format: { with: URI::MailTo::EMAIL_REGEXP }
Validating registrations in Rails
validations
i18n
error-handling
Intermediate
8 steps
python
import os from datetime import timedelta
Class-based config in a Flask app factory
configuration
app-factory
inheritance
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/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.