# Is there a way not to break 3 lines of code into 3 \<% %\>?

**URL:** <https://www.ruby-forum.com/t/is-there-a-way-not-to-break-3-lines-of-code-into-3/188560>\
**Category:** Rails\
**Created:** [May 13, 2010, 10:37pm UTC](https://www.ruby-forum.com/t/is-there-a-way-not-to-break-3-lines-of-code-into-3/188560 "2010-05-13T22:37:22Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![winterheat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7993a0/32.png) [@winterheat](https://www.ruby-forum.com/u/winterheat)\
**Post date:** [May 13, 2010, 10:37pm UTC](https://www.ruby-forum.com/t/is-there-a-way-not-to-break-3-lines-of-code-into-3/188560/1 "2010-05-13T22:37:22Z")

</div>

In the view code, say it is

\<% @stories.each do |s| %\>  
\<%= “

#{h s.inspect}
” %\>  
\<% end %\>

it would be breaking the code into 3 \<% %\> and \<%= %\>

is there a way to just have one \<% %\> or \<%= %\> so as to keep the code  
more flowing together?

---

<div class="post-metadata">

**Author:** ![winterheat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7993a0/32.png) [@winterheat](https://www.ruby-forum.com/u/winterheat)\
**Post date:** [May 13, 2010, 10:43pm UTC](https://www.ruby-forum.com/t/is-there-a-way-not-to-break-3-lines-of-code-into-3/188560/2 "2010-05-13T22:43:00Z")

</div>

> In the view code, say it is
> 
> \<% @stories.each do |s| %\>  
> \<%= “
> 
> #{h s.inspect}
> ” %\>  
> \<% end %\>
> 
> it would be breaking the code into 3 \<% %\> and \<%= %\>
> 
> is there a way to just have one \<% %\> or \<%= %\> so as to keep the code  
> more flowing together?

The above is pretty standard (I’d indent it though). You can change the  
%\> to -%\> to kill off new lines in the output.

In the above case you could do this…

\<%= @stories.map {|s| content\_tag(:div, h(s.inspect)) }.join %\>

Not sure that’s more readable or not… I’ll do that sometimes for  
things like ‘li’ entries.

-philip

---

<div class="post-metadata">

**Author:** ![winterheat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7993a0/32.png) [@winterheat](https://www.ruby-forum.com/u/winterheat)\
**Post date:** [May 13, 2010, 10:57pm UTC](https://www.ruby-forum.com/t/is-there-a-way-not-to-break-3-lines-of-code-into-3/188560/3 "2010-05-13T22:57:00Z")

</div>

Jian L. wrote:

> using join looks like will create only n + 1 string objects…

ah, on second thought, doesn’t the join() method actually will create n  
string objects too? (the next one longer than a previous one), so it  
would be creating 2n string objects too.

---

<div class="post-metadata">

**Author:** ![winterheat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7993a0/32.png) [@winterheat](https://www.ruby-forum.com/u/winterheat)\
**Post date:** [May 13, 2010, 10:51pm UTC](https://www.ruby-forum.com/t/is-there-a-way-not-to-break-3-lines-of-code-into-3/188560/4 "2010-05-13T22:51:13Z")

</div>

Philip H. wrote:

> In the above case you could do this…
> 
> \<%= @stories.map {|s| content\_tag(:div, h(s.inspect)) }.join %\>

i was at first worried that if the print out is long, like a few hundred  
lines, then it can create many string objects.

using join looks like will create only n + 1 string objects…

if using inject or something, it might be creating about 2n string  
objects, each one longer than the previous ones.

that’s why is it true that  
“some text #{ } and then something more #{ }”

is better since it is one string object created while  
"some text " + expression + " and then something more " + expression

will create 4 string objects and therefore running slower?

---

<div class="post-metadata">

**Author:** ![winterheat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7993a0/32.png) [@winterheat](https://www.ruby-forum.com/u/winterheat)\
**Post date:** [May 13, 2010, 11:26pm UTC](https://www.ruby-forum.com/t/is-there-a-way-not-to-break-3-lines-of-code-into-3/188560/5 "2010-05-13T23:26:39Z")

</div>

Philip H. wrote:

> > In the view code, say it is
> > 
> > \<% @stories.each do |s| %\>  
> > \<%= “
> > 
> > #{h s.inspect}
> > ” %\>  
> > \<% end %\>

Another option, although probably not necessary in this case, is to move  
the code into a view helper.

\<%= h story\_list(@stories) %\>

def story\_list(stories)  
list = “”  
stories.each do |s|  
list += content\_tag(:div, h(s.inspect))  
end  
list  
end

Yes, in this case the helper is more verbose (maybe there’s a better way  
that what I did), but at least your view code is clean and pretty. The  
ugliness get move outside the view into the helper.

Another common pattern for a case like you show is to move the code that  
actually lists the stories into a partial. Then rendering the partial  
from your view becomes one line:

\<%= render :partial =\> @stories %\>

---

<div class="post-metadata">

**Author:** ![winterheat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7993a0/32.png) [@winterheat](https://www.ruby-forum.com/u/winterheat)\
**Post date:** [May 13, 2010, 11:33pm UTC](https://www.ruby-forum.com/t/is-there-a-way-not-to-break-3-lines-of-code-into-3/188560/6 "2010-05-13T23:33:45Z")

</div>

Robert W. wrote:

> Philip H. wrote:  
> def story\_list(stories)  
> list = “”  
> stories.each do |s|  
> list += content\_tag(:div, h(s.inspect))  
> end  
> list  
> end

Well this was a bit lame. Use the map & join as shown from previous  
posts:

> \<%= @stories.map {|s| content\_tag(:div, h(s.inspect)) }.join %\>

I wasn’t thinking.
