Ruby's ERB templates are handy things you can use in a variety of places beyond using for web / Ruby on Rails context.

Since ERB is built into Ruby no additional gems / dependencies needs to be installed beyond Ruby.

Quick ref guide to enable ruby / ruby output / comments in ERB template file:

<%   %>   # execute Ruby code inside brackets (no output)
<%=  %>   # execute Ruby code and prints the output into template
<%== %>   # execute and prints something verbatim (i.e. w/o escaping)
<%  -%>   # executes / avoid line break after expression
<%#  %>   # comments out code within the brackets

AI Ruby Erb Guide

Below is output from when I asked Chat GPT to provide a simple explanation with examples that shows how to create and use a Markdown ERb template in a Ruby script to pass simple data structures into a Markdown Erb to help speed run past the silly bumps in learning this concept.

1. Create the Markdown ERb Template

First, create a file named report-summary.md.erb. This file is your template that will mix static Markdown content with dynamic Ruby code. For example, your template might look like this:

---
time_generated: <%= time_generated %>
report_count: <%= reports.size %>
---

# Report Summary

| Name | Title |
| ---- | ----- |
<% reports.each do |report| %>
| [<%= report[:name] %>](#<%= report[:name].downcase.gsub(" ", "-") %>) | <%= report[:title] %> |
<% end %>

# Details

<% reports.each do |report| %>
## <a name="<%= report[:name].downcase.gsub(" ", "-") %>"></a><%= report[:name] %>
- **Title:** <%= report[:title] %>
- **Date:** <%= report[:date] %>
- **Pop:** <%= report[:pop] %>

<% end %>

Explanation:


2. Setup Your Ruby Script

In your Ruby script, you need to require the ERB library, load the template, and set up the variables (such as reports and time_generated) that your template expects.

Here’s a simple example:

require 'erb'
require 'time'

# Assume 'reports' is already defined and populated, for example:
reports = [
  { name: "Alpha", title: "Report Alpha", date: "2025-03-11", pop: 123 },
  { name: "Beta", title: "Report Beta", date: "2025-03-10", pop: 456 }
]

# Set a variable for the current time to be used in the template.
time_generated = Time.now

# Read the Markdown ERB template from the file.
template_content = File.read("report-summary.md.erb")

# Create a new ERB instance with the template content.
erb_template = ERB.new(template_content)

# Render the template. The 'binding' makes local variables (reports, time_generated) available inside the template.
output = erb_template.result(binding)

Explanation:


3. Write the Output to a File

After rendering the template, you’ll likely want to save the output as a Markdown file. You can do this with:

# Write the rendered output to a new file, e.g., report-summary.md.
File.open("report-summary.md", "w") do |file|
  file.write(output)
end

puts "Markdown report generated: report-summary.md"

Explanation:


Summary

  1. Create your template: Write a Markdown file with embedded Ruby code (ERb) to produce dynamic content.
  2. Setup your script: Require the necessary libraries, prepare your data (like the reports array), and load your template.
  3. Render and output: Process the template with ERB.new and binding, then write the result to a file.

This approach keeps your logic separate from presentation, and it’s a great introduction to using ERb with Markdown in Ruby.