Grick Zh wrote:
> Adam Stegman 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 "+".
3) 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.
--
Posted via http://www.ruby-forum.com/.
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
No comments:
Post a Comment