Today I Learned

hashrocket A Hashrocket project

Rendering ERB

If you have a string that contains ERB templating, you can quickly generate the resulting string with the following code snippet:

require 'erb'

some_template_string = <<-TEXT
The top
<% 5.times do |i| %>
Item <%= i + 1 %>
<% end %>
The bottom
TEXT

puts ERB.new(some_template_string).result

This will print the following to stdout:

The top
Item 1
Item 2
Item 3
Item 4
Item 5
The bottom

source

See More #ruby TILs
Looking for help? Each developer at Hashrocket has years of experience working with Ruby applications of all types and sizes. We're an active presence at Ruby conferences, have written some of the most popular gems, and have worked on many of the web's Ruby on Rails success stories. Contact us today to talk about your Ruby project.