# My fantasy: instant array of AR attributes?

**URL:** <https://www.ruby-forum.com/t/my-fantasy-instant-array-of-ar-attributes/80277>\
**Category:** Rails\
**Created:** [December 23, 2006, 1:58am UTC](https://www.ruby-forum.com/t/my-fantasy-instant-array-of-ar-attributes/80277 "2006-12-23T01:58:06Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jfoxny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a6a055/32.png) [@jfoxny](https://www.ruby-forum.com/u/jfoxny)\
**Post date:** [December 23, 2006, 1:58am UTC](https://www.ruby-forum.com/t/my-fantasy-instant-array-of-ar-attributes/80277/1 "2006-12-23T01:58:06Z")

</div>

If there’s a way to do this, I would be very happy:

let’s pretend that I have an array called folks holding several AR  
objects from a model called People:

folks = [ \<AR 1: name =\> “bob”, sex =\> “m”, age =\> 40\>,  
\<AR 2: name =\> “sam”, sex =\> “m”, age =\> 7\>,  
\<AR 3: name =\> “dee”, sex =\> “f”, age =\> 25\> ]

What i’ve always wanted to do was say something like:

folks.names

…and get an array back with [“bob”,“sam”,“dee”].

It seems kind of cumbersome to write a loop to do this, especially  
when RoR is supposed to be all neato and cool. Am I missing something  
obvious or do I really have to use a loop?

Thanks!

–  
Jason F. - removed\_email\_address@domain.invalid - [http://www.seethroo.us/](http://www.seethroo.us/)  
work: (310) 601-8454  
cell: (415) 254-4890  
AIM/Skype: jfrankov

---

<div class="post-metadata">

**Author:** ![jfoxny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a6a055/32.png) [@jfoxny](https://www.ruby-forum.com/u/jfoxny)\
**Post date:** [December 23, 2006, 2:04am UTC](https://www.ruby-forum.com/t/my-fantasy-instant-array-of-ar-attributes/80277/2 "2006-12-23T02:04:07Z")

</div>

You could go about it a number of ways.

I like this method

folks.collect! { |person| person.name}

Now folks is an array of all of their names…

On 12/22/06, Jason F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 

–  
seth at subimage interactive  
[http://www.subimage.com/sublog/](http://www.subimage.com/sublog/)

---

<div class="post-metadata">

**Author:** ![jfoxny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a6a055/32.png) [@jfoxny](https://www.ruby-forum.com/u/jfoxny)\
**Post date:** [December 23, 2006, 2:12am UTC](https://www.ruby-forum.com/t/my-fantasy-instant-array-of-ar-attributes/80277/3 "2006-12-23T02:12:08Z")

</div>

subimage interactive wrote:

> You could go about it a number of ways.
> 
> I like this method
> 
> folks.collect! { |person| person.name}

Doesn’t Rails provide a shortcut:

folks.collect(&:name)

?

–  
Phlip  
[Redirecting...](http://www.greencheese.us/ZeekLand) ← NOT a blog!!!

---

<div class="post-metadata">

**Author:** ![jfoxny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a6a055/32.png) [@jfoxny](https://www.ruby-forum.com/u/jfoxny)\
**Post date:** [December 23, 2006, 2:24am UTC](https://www.ruby-forum.com/t/my-fantasy-instant-array-of-ar-attributes/80277/4 "2006-12-23T02:24:09Z")

</div>

Hi –

On Fri, 22 Dec 2006, Phlip wrote:

> folks.collect(&:name)
> 
> ?

Yes, and actually the thing that makes it work (Symbol#to\_proc) is in  
1.9:

$ /usr/local/lib/ruby-cvs/bin/ruby -ve ‘puts [1,2,3].map(&:to\_f)’  
ruby 1.9.0 (2006-12-09 patchlevel 0) [i686-linux]  
1.0  
2.0  
3.0

David

–  
Q. What’s a good holiday present for the serious Rails developer?  
A. RUBY FOR RAILS by David A. Black ([Ruby for Rails](http://www.manning.com/black))  
aka The Ruby book for Rails developers!  
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?  
A. Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))

---

<div class="post-metadata">

**Author:** ![jfoxny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a6a055/32.png) [@jfoxny](https://www.ruby-forum.com/u/jfoxny)\
**Post date:** [December 23, 2006, 2:28am UTC](https://www.ruby-forum.com/t/my-fantasy-instant-array-of-ar-attributes/80277/5 "2006-12-23T02:28:10Z")

</div>

Even shorter:

names = folks.collect(&:name)

-christos

---

<div class="post-metadata">

**Author:** ![jfoxny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a6a055/32.png) [@jfoxny](https://www.ruby-forum.com/u/jfoxny)\
**Post date:** [December 23, 2006, 2:54am UTC](https://www.ruby-forum.com/t/my-fantasy-instant-array-of-ar-attributes/80277/6 "2006-12-23T02:54:16Z")

</div>

I love it! My fantasy has come true. Anyone got a handle on what the  
& does? I knew about collect but it alters the original array, which  
I want to avoid (shuold’ve said that in my first email, sorry).

Thanks to everyone for the suggestions!

-Jason

–  
Jason F. - removed\_email\_address@domain.invalid  
work: (310) 601-8454  
cell: (415) 254-4890  
AIM/Skype: jfrankov

---

<div class="post-metadata">

**Author:** ![jfoxny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a6a055/32.png) [@jfoxny](https://www.ruby-forum.com/u/jfoxny)\
**Post date:** [December 23, 2006, 3:04am UTC](https://www.ruby-forum.com/t/my-fantasy-instant-array-of-ar-attributes/80277/7 "2006-12-23T03:04:17Z")

</div>

Hi –

On Fri, 22 Dec 2006, Jason F. wrote:

> On Dec 22, 2006, at 5:27 PM, Christos Z. wrote:
> 
> > Even shorter:
> > 
> > names = folks.collect(&:name)
> 
> I love it! My fantasy has come true. Anyone got a handle on what the & does?  
> I knew about collect but it alters the original array, which I want to avoid  
> (shuold’ve said that in my first email, sorry).

collect (aka map) doesn’t alter the original array, unless you use the  
! version (collect! or map!). I think one of the people who replied  
did use that, but Christos’s version doesn’t.

The & introduces a lambda (an anonymous function, technically an  
instance of Proc), to be used as the code block to go with the method  
call. For example, look at this:

array = [1,2,3,4,5]  
array.map {|e| e \* 10 } # [10,20,30,40,50]

and now with a separate lambda:

lam = lambda {|e| e \* 10 }  
array.map &lam # [10,20,30,40,50]

The reason it works with &:name is that Rails has a to\_proc conversion  
method for symbols. That method gets called automatically when the &  
is applied. When you do &:name, you get a lambda that, if you wrote  
it out by hand, would look like this:

lambda {|e| e.send(:name) }

So – the net effect of all of this is that this:

folks.collect(&:name)

calls collect on folks, with a code block (a lambda) that performs the  
function of calling the “name” method on each element in folks. The  
result is a second, result array, consisting of all the names.

David

–  
Q. What’s a good holiday present for the serious Rails developer?  
A. RUBY FOR RAILS by David A. Black ([Ruby for Rails](http://www.manning.com/black))  
aka The Ruby book for Rails developers!  
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?  
A. Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))
