0Pricing
Ruby Academy · Lesson

Choosing a Format

Trade-offs.

Choosing a Format is a free Ruby Academy lesson on CoddyKit — lesson 4 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.

Three Formats, Three Jobs

You now know JSON, CSV, and YAML. Each shines in different situations. Picking the right one keeps your data easy to produce, store, and consume.

  • JSON for APIs and web
  • CSV for tabular/spreadsheet data
  • YAML for configuration
puts ['JSON', 'CSV', 'YAML'].join(', ')

JSON Strengths

JSON is compact, universally supported, and maps to nested Hashes and Arrays. It is the de facto standard for web APIs and data interchange between services.

require 'json'
puts({ id: 1, items: [1, 2] }.to_json)

JSON Weaknesses

JSON has no comments and is verbose for humans to hand-edit. Trailing commas and single quotes are invalid, which trips people up in config files.

require 'json'
begin
  JSON.parse('{"a":1,}')
rescue JSON::ParserError
  puts 'trailing comma not allowed'
end

CSV Strengths

CSV is perfect for flat, tabular data: rows and columns. Spreadsheets, exports, and bulk imports all speak CSV, and it is extremely compact for large tables.

require 'csv'
puts(CSV.generate { |c| c << ['a', 'b']; c << [1, 2] })

CSV Weaknesses

CSV cannot represent nested or hierarchical data. It has no standard type system, so everything is a string until you convert it, and dialects (separators, quoting) vary.

require 'csv'
row = CSV.parse_line('5,true')
puts(row[0].class)
puts(row[1].class)

YAML Strengths

YAML is the most human-readable, supports comments, and handles nesting cleanly. That makes it ideal for configuration files developers edit by hand.

require 'yaml'
text = "# app config\nport: 8081"
puts(YAML.load(text)['port'])

YAML Weaknesses

YAML's indentation rules are error-prone (a stray space breaks it), and unsafe loading can be a security risk. It is also slower to parse than JSON.

require 'yaml'
cfg = YAML.safe_load("port: 8081")
puts(cfg['port'])

Nesting Comparison

JSON and YAML both represent the same nested structure naturally; CSV cannot.

require 'json'
require 'yaml'
data = { 'user' => { 'name' => 'Ada', 'roles' => ['admin'] } }
puts(data.to_json)
puts(data.to_yaml)

Readability vs Compactness

Trade-off: YAML is most readable, JSON is balanced, CSV is most compact for tables. Choose based on whether humans or machines are the main consumer.

require 'json'
require 'yaml'
h = { 'a' => 1, 'b' => 2 }
puts("JSON len: #{h.to_json.length}")
puts("YAML len: #{h.to_yaml.length}")

A Decision Guide

Quick rules:

  • Talking to a web API? JSON
  • Exporting rows for a spreadsheet? CSV
  • App settings a human edits? YAML

When in doubt, JSON is the safest interchange default.

choice = { api: 'JSON', table: 'CSV', config: 'YAML' }
puts(choice[:config])

Converting Between Formats

Because all three parse into the same Ruby Hash/Array structures, converting is straightforward: parse one, dump another.

require 'json'
require 'yaml'
data = JSON.parse('{"port":8081,"name":"app"}')
puts(data.to_yaml)

Quick Check

Which format is the best default choice for a configuration file that developers edit by hand and want comments in?

Recap: Choosing a Format

You can now pick the right tool:

  • JSON: APIs, interchange, balanced
  • CSV: flat tables, spreadsheets, compact
  • YAML: config, readable, comments, nesting
  • All parse to the same Ruby structures, so conversion is easy
require 'yaml'
require 'json'
puts(YAML.load('a: 1').to_json)

Frequently asked questions

Is the “Choosing a Format” lesson free?

Yes — the full text of “Choosing a Format” 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 “Choosing a Format”?

Trade-offs. 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 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Choosing a Format” 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

  1. Reading and Writing JSON
  2. Working with CSV
  3. YAML Configuration
  4. Choosing a Format
← Back to Ruby Academy