Ruby
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Methods and Blocks
-
Section 4: Object-Oriented Ruby
-
Section 5: Metaprogramming
-
Section 6: Working with Data and Files
-
Section 7: Ruby Frameworks Overview
-
Section 8: Ecosystem and Testing
-
Section 9: Practical Projects
-
Section 10: Interview Practice
-
Section 11: Data Structures and Algorithms in Ruby
-
Section 12: More Practice Exercises
-
Section 13: Enumerable and Functional Style
-
Section 14: More OOP Practice
-
Section 15: More Testing
-
Section 16: Enumerable and Comparable Modules In Depth
-
Section 17: Ruby Standard Library: Core Utilities
-
Section 18: Ruby Standard Library: Data and Security
-
Section 19: Ruby Standard Library: CLI and Text
-
Section 20: Ruby Networking
-
Section 21: Ruby on Rails Deep Dive
-
Section 22: Ruby Metaprogramming Deep Dive
-
Section 23: Ruby Design Patterns
-
Section 24: Ruby Concurrency
-
Section 25: Ruby Testing Deep Dive
-
Section 26: Ruby Gems and Packaging
-
Section 27: Ruby Performance
-
Section 28: More Practice Exercises
-
Section 29: More Interview Practice
-
Section 30: Rails API Development
-
Section 31: Rails Authentication and Authorization
-
Section 32: Rails Testing Deep Dive
-
Section 33: Rails Performance
-
Section 34: Rails Deployment
-
Section 35: Sinatra and Lightweight Ruby Web Apps
-
Section 36: More Ruby Language Deep Dive
-
Section 37: Ruby 3.x Modern Features
-
Section 38: More Data Structures in Ruby
-
Section 39: More Practical Projects
-
Section 40: Ruby Ecosystem Tools
-
Section 41: More Practice and Review
-
Section 42: Final Practice and Mastery
-
Section 43: Ruby Interview Deep Dive
-
Section 44: Ruby Background Processing Deep Dive
-
Section 45: Ruby GraphQL
-
Section 46: Ruby Object Model Deep Dive
-
Section 47: Ruby Hanami Framework Overview
-
Section 48: Ruby gRPC and Protocol Buffers
-
Section 49: Ruby Data Processing
-
Section 50: Ruby Search Integration
-
Section 51: Ruby File Upload and Media
-
Section 52: Ruby Email and Notifications
-
Section 53: Ruby Admin Panels
-
Section 54: Ruby Feature Flags and Experimentation
-
Section 55: Ruby Monitoring and Observability
-
Section 56: Ruby Docker and Deployment Deep Dive
-
Section 57: Ruby Security Deep Dive
-
Section 58: More Advanced Metaprogramming
-
Section 59: More Final Projects
75: ERB Templating
What's the actual difference between <% and <%= ?
This is the first thing everyone trips over. It looks like a tiny difference, but it's the difference between your page being blank or actually showing data. Think of it this way: <% ... %> is for logic, and <%= ... %> is for output.
If you're writing an if statement or a each loop, you don't want the Ruby code itself to be printed onto the screen; you just want it to execute. That's where the silent tags come in. But when you want to actually drop a variable's value into the HTML, you add that equals sign.
<% if user_logged_in? %>
<p>Welcome back, <%= user_name %>!</p>
<% else %>
<p>Please log in to continue.</p>
<% end %>
I've spent way too many hours debugging a page only to realize I forgot the = on a variable. It happens to the best of us.
How do I handle a list of items without writing repetitive HTML?
You don't want to hardcode ten different <li> tags. Since ERB is just Ruby embedded in text, you can use any Ruby iterator you already know. I usually lean on .each for this. You wrap the HTML you want to repeat inside the loop tags, and ERB will handle the heavy lifting.
Let's say we're building a simple task list. Instead of manual entry, we'll iterate over an array of hashes:
<ul>
<% @tasks.each do |task| %>
<li>
<strong><%= task[:title] %></strong>
<span>— <%= task[:priority] %></span>
</li>
<% end %>
</ul>
The magic here is that everything inside the .each block—including the HTML tags—gets repeated for every single item in the @tasks array.
How do I actually get the Ruby data into the template?
This is where the "magic" happens behind the scenes. ERB doesn't automatically know about your local variables. To make it work, you have to pass the current binding to the ERB object. The binding is essentially a snapshot of the current scope—the variables, methods, and state available at that exact moment in your code.
Here is a complete, runnable example of how I usually set this up in a small script:
require 'erb'
# Our data
@product_name = "Mechanical Keyboard"
@price = 129.99
@features = ["RGB Lighting", "Brown Switches", "USB-C"]
# The template (usually this would be in a separate .erb file)
template = <% =ERB.new("<h1><%= @product_name %></h1>
<p>Price: $<%= @price %></p>
<ul>
<% @features.each do |f| %>
<li><%= f %></li>
<% end %>
</ul>") %>
# Render the template using the current binding
puts template.result(binding)
Without binding, the template would have no idea what @product_name is and would throw a NameError. I highly recommend keeping your logic in the Ruby class and using the ERB file strictly for presentation. If you find yourself writing complex calculations inside <% %>, it's a sign you should move that logic back into a Ruby method.
📋 Practical Task
Exercise: Building a Dynamic User Profile Page
Your goal is to create a small Ruby script that renders a user profile using ERB. You will need to handle a mix of data types and a conditional display.
- Create a hash named
userwith the following keys::name,:email,:is_premium(boolean), and:badges(an array of strings). - Create an ERB template string (or file) that:
- Displays the user's name in an
<h2>tag. - Displays the email in a paragraph.
- Uses an
ifstatement to display a "⭐ Premium Member" badge only if:is_premiumis true. - Iterates through the
:badgesarray to create an unordered list of the user's achievements.
- Displays the user's name in an
- Use
ERB.new(...).result(binding)to output the final HTML to the console.
There are no comments for now.