60

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.

Alpha Sisyphus
  • 1,508
  • 3
  • 19
  • 33

2 Answers2

80

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.

Simone Carletti
  • 173,507
  • 49
  • 363
  • 364
  • 3
    why is the minus syntax still in the docs? http://api.rubyonrails.org/classes/ActionView/Base.html – BrendanDean May 25 '12 at 15:24
  • 3
    It looks to be left in the docs so when someone encounters some legacy code and doesn't recognize it they have some place to find it's meaning. – OpenCoderX Jul 18 '12 at 23:14
  • (just passing by) Sorry but in HTML spaces are significant, and controlling the space between tags is important – Benjamin Bouchet Jul 25 '13 at 17:37
  • I agree with Benjamin. An example is elements with "display: inline-block", which are whitespace-dependent. http://stackoverflow.com/questions/5256533/a-space-between-inline-block-list-items – Coomer Feb 28 '14 at 23:49
  • 1
    I've verified that there's no difference in rendered output in rails 3+4, but why is that? The erb docs for ruby 2.1.1 still describe the different tag behaviour, as do the rails 4.0 docs. – James Healy Mar 21 '14 at 02:36
  • 1
    @JamesHealy Ruby2.1.1 description is for ERB. Rails3+4 used [erubis](http://www.kuwata-lab.com/erubis/) as the default engine, and describe as following: When on a line that only contains whitespaces except for the tag, <% %> suppress leading and trailing whitespace, including the trailing newline. <% %> and <%- -%> are the same. Note however that <%= %> and <%= -%> are different: only the latter removes trailing whitespaces. – lingceng Feb 05 '15 at 09:54
7

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.

noodl
  • 17,143
  • 3
  • 57
  • 55