# Collections with to\_proc

**URL:** <https://www.ruby-forum.com/t/collections-with-to-proc/132969>\
**Category:** Rails\
**Created:** [March 26, 2008, 9:37am UTC](https://www.ruby-forum.com/t/collections-with-to-proc/132969 "2008-03-26T09:37:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![grayson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c77e96/32.png) [@grayson](https://www.ruby-forum.com/u/grayson)\
**Post date:** [March 26, 2008, 9:37am UTC](https://www.ruby-forum.com/t/collections-with-to-proc/132969/1 "2008-03-26T09:37:47Z")

</div>

why does this work?

@users.select {|u| u.name == ‘Bob’}

but this doesn’t

@user.select(&:name == ‘Bob’)

Thanks.

---

<div class="post-metadata">

**Author:** ![grayson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c77e96/32.png) [@grayson](https://www.ruby-forum.com/u/grayson)\
**Post date:** [March 26, 2008, 10:06am UTC](https://www.ruby-forum.com/t/collections-with-to-proc/132969/2 "2008-03-26T10:06:04Z")

</div>

On 26 Mar 2008, at 08:37, Grayson P. wrote:

> why does this work?
> 
> @users.select {|u| u.name == ‘Bob’}
> 
> but this doesn’t
> 
> @user.select(&:name == ‘Bob’)

Because that evaluates as @user.select(&(:name == ‘Bob’)) ie  
@user.select &false and false doesn’t have a to\_proc method.  
The Symbol#to\_proc is convenient shorthand but it’s not a catch all.

Fred

---

<div class="post-metadata">

**Author:** ![grayson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c77e96/32.png) [@grayson](https://www.ruby-forum.com/u/grayson)\
**Post date:** [March 26, 2008, 6:02pm UTC](https://www.ruby-forum.com/t/collections-with-to-proc/132969/3 "2008-03-26T18:02:05Z")

</div>

Fred,

Thanks for the reply. I’m not sure I understand the response. By that  
logic the following also evaluates to false but it seem to work.

@user.select(&:active?)

Are you saying this syntax is impossible for what I’m trying to do? The  
issue I have is that I already have this statement embedded in an inject  
inside an HTML construct and it’s to many {}

Thanks

GP

Frederick C. wrote:

> On 26 Mar 2008, at 08:37, Grayson P. wrote:
> 
> > why does this work?
> > 
> > @users.select {|u| u.name == ‘Bob’}
> > 
> > but this doesn’t
> > 
> > @user.select(&:name == ‘Bob’)
> 
> Because that evaluates as @user.select(&(:name == ‘Bob’)) ie  
> @user.select &false and false doesn’t have a to\_proc method.  
> The Symbol#to\_proc is convenient shorthand but it’s not a catch all.
> 
> Fred

---

<div class="post-metadata">

**Author:** ![grayson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c77e96/32.png) [@grayson](https://www.ruby-forum.com/u/grayson)\
**Post date:** [March 26, 2008, 6:11pm UTC](https://www.ruby-forum.com/t/collections-with-to-proc/132969/4 "2008-03-26T18:11:48Z")

</div>

On 26 Mar 2008, at 17:02, Grayson P. wrote:

> The  
> issue I have is that I already have this statement embedded in an  
> inject  
> inside an HTML construct and it’s to many {}

I thing you need to understand what’s going on behind the scenes. You  
could just write  
@user.select {|u| u.active?} but that’s a bit tedious  
When you pass to a method an argument prefixed by & that tell ruby  
'this is a proc and you should use this proc as the block for this  
method. For example, this also words  
p = lambda {|u| u.active?}  
@user.select(&p)

Ruby being ruby, it doesn’t really care if what you pass is actually a  
proc, it just needs to look enough like one and will call to\_proc on  
such arguments.  
What people have done is define Symbol#to\_proc in such a way that it  
creates a proc that does the right thing

If we were to limit ourselves to the case where the block takes a  
single argument it’s as simple as

def to\_proc  
Proc.new({|arg| arg.send(self)  
end  
It’s a bit more complicated to cope with when the block is passed  
multiple arguments

This fundamentally can’t handle something like &:name == ‘Bob’, like I  
said it ends up trying to call to\_proc on false.

Fred
