# Problem about "each" in rails

**URL:** <https://www.ruby-forum.com/t/problem-about-each-in-rails/185356>\
**Category:** Rails\
**Created:** [March 18, 2010, 5:33pm UTC](https://www.ruby-forum.com/t/problem-about-each-in-rails/185356 "2010-03-18T17:33:32Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![pengzhang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/7feea3/32.png) [@pengzhang](https://www.ruby-forum.com/u/pengzhang)\
**Post date:** [March 18, 2010, 5:33pm UTC](https://www.ruby-forum.com/t/problem-about-each-in-rails/185356/1 "2010-03-18T17:33:32Z")

</div>

CODE1:

\<% @posts.each do |p|%\>  
\<%=h p.name %\>  
\<% end %\>

this will show the name string like “Tom” in the web page.

CODE2:

\<%=h @posts.each {|p| p.name} %\>

why this code return serial strings like #Post:0x3a893e8 ?

---

<div class="post-metadata">

**Author:** ![pengzhang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/7feea3/32.png) [@pengzhang](https://www.ruby-forum.com/u/pengzhang)\
**Post date:** [March 18, 2010, 5:49pm UTC](https://www.ruby-forum.com/t/problem-about-each-in-rails/185356/2 "2010-03-18T17:49:50Z")

</div>

On 18 March 2010 16:33, Grick Zh [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> \<%=h @posts.each {|p| p.name} %\>
> 
> why this code return serial strings like #Post:0x3a893e8 ?

Because you’re outputting the return value of the whole loop, not the  
output of each block.  
If you run that command in your console, you will see it return an  
array of Posts; and that’s what you’re trying to render.

---

<div class="post-metadata">

**Author:** ![pengzhang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/7feea3/32.png) [@pengzhang](https://www.ruby-forum.com/u/pengzhang)\
**Post date:** [March 18, 2010, 6:57pm UTC](https://www.ruby-forum.com/t/problem-about-each-in-rails/185356/3 "2010-03-18T18:57:46Z")

</div>

If you want the names seperated by comma, try this:

\<%=h @posts.map{|p| p.name}.join(’, ') %\>

---

<div class="post-metadata">

**Author:** ![pengzhang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/7feea3/32.png) [@pengzhang](https://www.ruby-forum.com/u/pengzhang)\
**Post date:** [March 19, 2010, 3:54pm UTC](https://www.ruby-forum.com/t/problem-about-each-in-rails/185356/4 "2010-03-19T15:54:07Z")

</div>

Michael P. wrote:

> On 18 March 2010 16:33, Grick Zh [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > \<%=h @posts.each {|p| p.name} %\>
> > 
> > why this code return serial strings like #Post:0x3a893e8 ?
> 
> Because you’re outputting the return value of the whole loop, not the  
> output of each block.  
> If you run that command in your console, you will see it return an  
> array of Posts; and that’s what you’re trying to render.

I got it,thanks for your help 🙂

---

<div class="post-metadata">

**Author:** ![pengzhang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/7feea3/32.png) [@pengzhang](https://www.ruby-forum.com/u/pengzhang)\
**Post date:** [March 19, 2010, 4:14pm UTC](https://www.ruby-forum.com/t/problem-about-each-in-rails/185356/5 "2010-03-19T16:14:20Z")

</div>

Adam S. wrote:

> Or more concisely (because it’s fun):  
> \<%=h @posts.map(&:name).to\_sentence %\>  
> which will make it “Tom, Dick, and Harry”.

I’m newbie,can’t understand “(&:name)”,but i know this code is skillful.

---

<div class="post-metadata">

**Author:** ![pengzhang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/7feea3/32.png) [@pengzhang](https://www.ruby-forum.com/u/pengzhang)\
**Post date:** [March 19, 2010, 2:36pm UTC](https://www.ruby-forum.com/t/problem-about-each-in-rails/185356/6 "2010-03-19T14:36:05Z")

</div>

Or more concisely (because it’s fun):  
\<%=h @posts.map(&:name).to\_sentence %\>  
which will make it “Tom, Dick, and Harry”.

---

<div class="post-metadata">

**Author:** ![pengzhang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/7feea3/32.png) [@pengzhang](https://www.ruby-forum.com/u/pengzhang)\
**Post date:** [March 19, 2010, 4:27pm UTC](https://www.ruby-forum.com/t/problem-about-each-in-rails/185356/7 "2010-03-19T16:27:04Z")

</div>

Grick Zh wrote:

> Adam S. wrote:
> 
> > Or more concisely (because it’s fun):  
> > \<%=h @posts.map(&:name).to\_sentence %\>  
> > which will make it “Tom, Dick, and Harry”.

1. @posts.map  
This will return a new enumerable, replacing each element of @posts with  
whatever the block says
2. &:name  
Shortcut to indicate a block and a method. In this case, the method is  
:name (post.name)

Other example: [1,2,3,4].inject(:+) # Sum every element of the array.  
Will of course fail if an element does not understand “+”.

1. to\_sentence  
We now have an array with names. “to\_sentence” is, I believe, an  
activesupport method which joins the array into a string with commas,  
and adds "and " between the second to last and last item.
