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.

About Aimred

Aimred is a specialist Ruby and Ruby on Rails development house and consultancy based in Cape Town, South Africa.

We provide Ruby and Ruby on Rails development, consulting and training services to businesses and organisations of all sizes. If you want to find out how we can help you, contact us at info@aimred.com.

Recent Posts

Yearly Archives