# Do not understand collect method use wit property

**URL:** <https://www.ruby-forum.com/t/do-not-understand-collect-method-use-wit-property/162595>\
**Category:** Rails\
**Created:** [March 11, 2009, 6:35pm UTC](https://www.ruby-forum.com/t/do-not-understand-collect-method-use-wit-property/162595 "2009-03-11T18:35:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![chisa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ba9def/32.png) [@chisa](https://www.ruby-forum.com/u/chisa)\
**Post date:** [March 11, 2009, 6:35pm UTC](https://www.ruby-forum.com/t/do-not-understand-collect-method-use-wit-property/162595/1 "2009-03-11T18:35:36Z")

</div>

Hi all,  
Can someone explain to me in terms of ruby why this works?

Story.find\_all\_by\_user\_id(2).collect(&:link) # 1 method call

This is in ROR console. link is a property of Story. The above command  
finds all db rows and all their properties (columns) with user\_id == 2  
and creates an array with only the link property.

This is same as:  
Story.find\_all\_by\_user\_id(2).collect{|x| x.link} # 2 block

I understand function #2 with use of a block.

I do not understand function #1 in terms of syntax. What is &:link? I  
know it represents a property and could accept that this is the way it  
works but I want to understand this in terms of ruby syntax. Is &:link  
a block? If so it would have to represent x.link. I do not understand  
how this is generated in ruby.

TIA,  
Pete

---

<div class="post-metadata">

**Author:** ![chisa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ba9def/32.png) [@chisa](https://www.ruby-forum.com/u/chisa)\
**Post date:** [March 11, 2009, 7:03pm UTC](https://www.ruby-forum.com/t/do-not-understand-collect-method-use-wit-property/162595/2 "2009-03-11T19:03:19Z")

</div>

They are both equivalent in terms of functionality.

The first is a common rails idiom:

@some\_collection.collect(&:id)  
or  
@some\_collection.map(&:id) #I’ve seen this one used more than  
collect…

The ampersand syntax is simply telling the interpreter that the symbol  
is the block parameter to the method. Internally the to\_proc method of  
the symbol class is called.

Here’s a reference:  
[http://apidock.com/rails/Symbol/to\_proc](http://apidock.com/rails/Symbol/to_proc)

Beware, doing @foo.map(&:id) doesn’t perform as well as @foo.map { |f|  
f.id } for big numbers of foos…  
Also note that this is a Rails only feature (ie: try it on plain irb  
and it will not work).

On Mar 11, 1:35 pm, Hiro P. \<rails-mailing-l…@andreas-

---

<div class="post-metadata">

**Author:** ![chisa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ba9def/32.png) [@chisa](https://www.ruby-forum.com/u/chisa)\
**Post date:** [March 11, 2009, 7:21pm UTC](https://www.ruby-forum.com/t/do-not-understand-collect-method-use-wit-property/162595/3 "2009-03-11T19:21:56Z")

</div>

Cool. Thanks for explanation.

Pete

Harold wrote:

> They are both equivalent in terms of functionality.
> 
> The first is a common rails idiom:
> 
> @some\_collection.collect(&:id)  
> or  
> @some\_collection.map(&:id) #I’ve seen this one used more than  
> collect…
> 
> The ampersand syntax is simply telling the interpreter that the symbol  
> is the block parameter to the method. Internally the to\_proc method of  
> the symbol class is called.
> 
> Here’s a reference:  
> [to\_proc (Symbol) - APIdock](http://apidock.com/rails/Symbol/to_proc)
> 
> Beware, doing @foo.map(&:id) doesn’t perform as well as @foo.map { |f|  
> f.id } for big numbers of foos…  
> Also note that this is a Rails only feature (ie: try it on plain irb  
> and it will not work).
> 
> On Mar 11, 1:35ï¿½pm, Hiro P. \<rails-mailing-l…@andreas-
