'-%>' (minus sign) at the end of a ERb sequence

Ruby on-RailsRuby

Ruby on-Rails Problem Overview


What's the point of using '-' (minus sign) at the end of a ERb sequence?

Example:

<% 3.times do -%>
  FooBar<br />
<% end -%>
Sometext

Regardless of whether I use '-' or not, a browser renders the same output.

Thanks, Aplha.

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

Before Rails 3, ERB adds additional spaces before and after the value when rendering the HTML content. In HTML spaces are not significant, except when you are using special tags such as <pre>.

Using the - sign forced ERB to avoid additional spaces.

This is completely useless in Rails 3.

Solution 2 - Ruby on-Rails

Using a minus sign on the opening or closing part of an erb tag suppresses whitespace before or after the tag on that line.

It was mainly useful when generating things like text/plain emails with erb but like @Simone pointed out, it's now moot.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAlpha SisyphusView Question on Stackoverflow
Solution 1 - Ruby on-RailsSimone CarlettiView Answer on Stackoverflow
Solution 2 - Ruby on-RailsnoodlView Answer on Stackoverflow