What is the recommended use of ERb in Rails when it comes to <% %> (evaluate Ruby code), <% -%> (evaluate Ruby code, suppress the trailing newline) and <%- -%> (evaluate Ruby code, suppress the trailing newline and leading space)? It seems like <%- -%> would make the output HTML look nicest, but <% %> seems to be mostly what I see.
 
    
    - 32,230
- 28
- 81
- 120
- 
                    IMHO, it doesn't really matter how "nice" the HTML looks as long as there is some visible indented structure to the generated HTML, I don't bother with supressing newlines etc. – Zabba Nov 08 '10 at 19:49
- 
                    1I think <% %> and <% -%> are synonymous in rails 3 (no trailing newline) – raidfive Nov 09 '10 at 00:52
- 
                    9I think you're [shaving yaks](http://projects.csail.mit.edu/gsb/old-archive/gsb-archive/gsb2000-02-11.html). There are many things to worry about when coding but whether your emitted HTML looks good is not real high on the list of things to do that make a big difference. But, don't feel bad, because you're in good company. We all do it. – the Tin Man Dec 17 '10 at 03:44
- 
                    @Greg +1 to your comment for teaching me the term "yak shaving". – jrdioko Dec 17 '10 at 04:04
- 
                    LOL. Yep. It's kind of disturbing to learn there's a term for it. I always thought it was called procrastinating. Now go watch the Ren and Stimpy show to find the episode. :-) – the Tin Man Dec 17 '10 at 04:07
- 
                    Don't underestimate the effect of clean, readable and therefore easier to debug code on developer's need for elegance. In my opinion, the result of your yak shaving might be a prime motivator for some people maintaining your website. Then again, there are surely more effective ways to make HTML more readable than manually through ERB tweaking. – aef Jan 30 '20 at 12:06
3 Answers
It's a personal preference. I use <% %> when I'm writing a loop or a block, because I want new lines there. I use <% -%> in rare cases of variable assignment. And I never use <%- -%> because that's one option too many.
 
    
    - 1,217
- 1
- 10
- 14
- 
                    1`<%- if foo? ->` is just what you want in a rails generator template – Chris Beck Oct 29 '15 at 00:08
I just read in http://ruby-doc.org/ruby-1.9/classes/ERB.html that you can even use a single percent sign for oneliners (if there is nothing else on that line)
Example from the docs:
<%# ignore numerous minor requests -- focus on priorities %>
% priorities.each do |priority|
  * <%= priority %>
% end
aaaalmost like HAML, isn't it? :)
 
    
    - 199
- 8
I don't think the trailing hyphen -%> works any more; I just got an error trying to use it in Ruby 2.6.6. However, @onetom's suggestion of using just a percent % for an entire line of code works, it outputs nothing, and doesn't keep the unwanted newline at the end.
Examples:
Trailing hyphen
line1
<%- sought = 'pattern' -%>
line3
Error message:
file.erb:1: syntax error, unexpected ';' (SyntaxError)
; - sought = 'pattern' -; _erbout.<< "\n".freeze
No trailing hyphen:
line1
<%- sought = 'pattern' %>
line3
Output:
line1
line3
Percent only:
line1
% sought = 'pattern'
line3
Output:
line1
line3
 
    
    - 2,818
- 1
- 26
- 40