Making ERb (Slightly) Less Ugly
In researching my previous article on code generation I came across a feature of ERb I’d never known about which makes ERb a bit more pleasing to the eye. Usually when I’ve written ERb template in the past it has looked like
1 <% 5.times do |i| %> 2 <%= i %>) hello 3 <% end %>
but if you instantiate ERb with the correct trim_mode string like so
1 ERB.new( template, nil, "%" )
then you can rewrite the previous template as
1 % 5.times do 2 <%= i %>) hello 3 % end
Farrel Lifson is a lead developer at Aimred.