Challenge - 5 Problems
ERB Syntax Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
Output of ERB expression tags
What will be rendered on the page by this ERB snippet?
<% name = 'Alice' %>
<%= "Hello, #{name}!" %>
<%# This is a comment %>
<% name = 'Bob' %>
<%= "Goodbye, #{name}!" %>Ruby on Rails
<% name = 'Alice' %> <%= "Hello, #{name}!" %> <%# This is a comment %> <% name = 'Bob' %> <%= "Goodbye, #{name}!" %>
Attempts:
2 left
💡 Hint
Remember that <% %> runs Ruby code without output, and <%= %> outputs the result.
✗ Incorrect
The first <% name = 'Alice' %> sets name to 'Alice'. Then <%= "Hello, #{name}!" %> outputs 'Hello, Alice!'. The comment <%# ... %> is ignored. Then name is changed to 'Bob'. The last <%= ... %> outputs 'Goodbye, Bob!'.
📝 Syntax
intermediate1:30remaining
Correct ERB tag for Ruby code without output
Which ERB tag correctly runs Ruby code but does NOT output anything to the page?
Attempts:
2 left
💡 Hint
Think about which tag runs code silently.
✗ Incorrect
The <% %> tag runs Ruby code but does not output anything. The <%= %> tag outputs the result. The <%# %> is a comment and does not run code. The <%== %> is invalid syntax.
🔧 Debug
advanced2:30remaining
Identify the ERB syntax error
Which option contains an ERB syntax error that will cause the template to fail rendering?
Ruby on Rails
<%= 'Hello' %> <% if true %> <%= 'Yes' %> <% end %>
Attempts:
2 left
💡 Hint
Check which tag is used to start the if statement.
✗ Incorrect
Option C uses <%= if user_signed_in? %> which tries to output the if statement itself, causing a syntax error. The correct way is to use <% if ... %> without = to run code without output.
❓ state_output
advanced2:00remaining
Value of variable after ERB execution
Given this ERB snippet, what is the value of
count after rendering?<% count = 0 %> <% 3.times do %> <% count += 1 %> <% end %> <%= count %>
Ruby on Rails
<% count = 0 %> <% 3.times do %> <% count += 1 %> <% end %> <%= count %>
Attempts:
2 left
💡 Hint
Remember that Ruby code inside <% %> runs and can change variables.
✗ Incorrect
The variable count starts at 0. The loop runs 3 times, each time adding 1 to count. So count becomes 3. The <%= count %> outputs 3.
🧠 Conceptual
expert2:30remaining
Difference between <%= and <%== in ERB
What is the main difference between the ERB tags
<%= %> and <%== %> when rendering HTML content?Attempts:
2 left
💡 Hint
Think about safe HTML rendering and escaping.
✗ Incorrect
The <%= %> tag escapes HTML entities to prevent injection attacks, so it outputs safe text. The <%== %> tag outputs raw HTML without escaping, useful when you want to render HTML tags directly.