YAML Configuration
Load and dump YAML.
YAML Configuration is a free Ruby Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Ruby Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Is YAML?
YAML (YAML Ain't Markup Language) is a human-friendly data format widely used for configuration. Ruby reads it via the yaml library (built on Psych).
require 'yaml'
text = "name: Ruby\nyear: 1995"
p YAML.load(text)Loading YAML
YAML.load parses a YAML string into Ruby objects. Mappings become Hashes, sequences become Arrays.
require 'yaml'
cfg = YAML.load("port: 8081\ndebug: true")
puts(cfg['port'])
puts(cfg['debug'])Indentation Defines Structure
YAML uses indentation (spaces, never tabs) to express nesting. Each deeper level is more indented.
require 'yaml'
text = "server:\n host: localhost\n port: 8081"
cfg = YAML.load(text)
puts(cfg['server']['port'])Lists in YAML
A sequence is written with leading dashes. It parses into a Ruby Array.
require 'yaml'
text = "tags:\n - fun\n - oop\n - dynamic"
p YAML.load(text)['tags']Dumping to YAML
YAML.dump (or to_yaml) serializes Ruby objects back into YAML text.
require 'yaml'
data = { 'name' => 'Ruby', 'tags' => ['fun', 'oop'] }
puts(YAML.dump(data))Scalar Types
YAML infers types: numbers, booleans (true/false), and null map to the matching Ruby values.
require 'yaml'
cfg = YAML.load("count: 3\nactive: false\nnote: null")
puts(cfg['count'].class)
puts(cfg['note'].inspect)Symbol Keys
A key written with a leading colon becomes a Ruby Symbol when loaded.
require 'yaml'
cfg = YAML.load(":env: production")
p cfgMultiple Documents
A single YAML file can hold several documents separated by ---. YAML.load_stream returns them all.
require 'yaml'
text = "a: 1\n---\nb: 2"
docs = YAML.load_stream(text)
puts(docs.length)Writing a Config File
Persist a settings hash as a readable YAML file.
require 'yaml'
require 'tmpdir'
path = File.join(Dir.tmpdir, 'config.yml')
File.write(path, { 'port' => 8081 }.to_yaml)
puts(File.read(path))Reading a Config File
YAML.load_file reads and parses a file in one call, the typical way apps load configuration.
require 'yaml'
require 'tmpdir'
path = File.join(Dir.tmpdir, 'app.yml')
File.write(path, "name: AppPanel\nport: 8081")
cfg = YAML.load_file(path)
puts(cfg['name'])Safe Loading
Loading untrusted YAML can be unsafe. YAML.safe_load restricts to basic types by default, protecting against malicious payloads.
require 'yaml'
cfg = YAML.safe_load("port: 8081\nname: app")
puts(cfg['port'])Quick Check
What does YAML use to express nesting and structure?
Recap: YAML Configuration
You can manage config in YAML:
YAML.load/load_fileto readto_yaml/YAML.dumpto write- Indentation defines nesting, dashes make lists
- Scalars infer numbers, booleans, null
- Prefer
safe_loadfor untrusted input
require 'yaml'
puts(YAML.load("x: 5")['x'] * 2)Frequently asked questions
Is the “YAML Configuration” lesson free?
Yes — the full text of “YAML Configuration” is free to read here on the web, and the Ruby Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Ruby Academy course, upgrade to CoddyKit PRO.
What will I learn in “YAML Configuration”?
Load and dump YAML. You practise Ruby Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Ruby Academy?
No prior experience is required. Ruby Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “YAML Configuration” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Ruby Academy lesson?
Yes. Every Ruby Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Reading and Writing JSON
- Working with CSV
- YAML Configuration
- Choosing a Format