# Erb:how to make less %?

**URL:** <https://www.ruby-forum.com/t/erb-how-to-make-less/177842>\
**Category:** Ruby\
**Created:** [October 25, 2009, 6:54am UTC](https://www.ruby-forum.com/t/erb-how-to-make-less/177842 "2009-10-25T06:54:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![haoqi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ed655f/32.png) [@haoqi](https://www.ruby-forum.com/u/haoqi)\
**Post date:** [October 25, 2009, 6:54am UTC](https://www.ruby-forum.com/t/erb-how-to-make-less/177842/1 "2009-10-25T06:54:05Z")

</div>

demo.html.erb  
\<% 3.times do%\>  
\<%=rand%\>  
\<%end%\>  
it has 6 ‘%’!  
how to make less ‘%’ like this?  
\<%  
3.times do  
puts rand  
end  
%\>

---

<div class="post-metadata">

**Author:** ![haoqi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ed655f/32.png) [@haoqi](https://www.ruby-forum.com/u/haoqi)\
**Post date:** [October 25, 2009, 11:37am UTC](https://www.ruby-forum.com/t/erb-how-to-make-less/177842/2 "2009-10-25T11:37:22Z")

</div>

Another option to look at is StringIO, so that you keep IO-like methods  
such as “puts”. You could also wrap \_erbout this way:

$ irb --simple-prompt

> > require ‘erb’  
> > =\> true
> 
> > require ‘stringio’  
> > =\> true
> 
> > ERB.new(\<\<‘EOS’).result(binding)  
> > \<% out = StringIO.new(\_erbout) %\>  
> > \<%  
> > 3.times do  
> > out.puts rand  
> > end  
> > %\>  
> > EOS  
> > =\> “0.761865648122361\n0.792800150151016\n0.683615203721222\n\n”
> 
> >

---

<div class="post-metadata">

**Author:** ![haoqi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ed655f/32.png) [@haoqi](https://www.ruby-forum.com/u/haoqi)\
**Post date:** [October 25, 2009, 11:28am UTC](https://www.ruby-forum.com/t/erb-how-to-make-less/177842/3 "2009-10-25T11:28:52Z")

</div>

Haoqi H. wrote:

> demo.html.erb  
> \<% 3.times do%\>  
> \<%=rand%\>  
> \<%end%\>  
> it has 6 ‘%’!  
> how to make less ‘%’ like this?  
> \<%  
> 3.times do  
> puts rand  
> end  
> %\>

Option 1:

\<%=  
res=“”  
3.times do  
res \<\< “#{rand}\n”  
end  
res  
%\>

(and you can make a helper method to abstract out this pattern)

Option 2:

\<%  
3.times do  
\_erbout \<\< “#{rand}\n”  
end  
%\>

This is more fragile, as the name of the \_erbout variable can be changed  
at runtime, and may not be compatible with other ERB implementations  
(e.g. erubis)  
without specific configuration. See  
[http://www.kuwata-lab.com/erubis/users-guide.03.html#erbout-enhancer](http://www.kuwata-lab.com/erubis/users-guide.03.html#erbout-enhancer)

Option 3:  
Use erubis with PrintEnabledEnhancer. See  
[http://www.kuwata-lab.com/erubis/users-guide.03.html#printenabled-enhancer](http://www.kuwata-lab.com/erubis/users-guide.03.html#printenabled-enhancer)  
I don’t know if this enables “puts” as well as “print”, and it’s an  
erubis-specific function.

Option 4:  
Ditch ERB entirely and switch to HAML. Your example becomes just:

- 3.times do  
= rand

You’ll either love HAML or hate it. Personally I love it. My templates  
are a fraction of the size they were before, and I never have to worry  
about unbalanced XHTML tags or missing ‘end’ on a block.

HTH,

Brian.
