Forum: Ruby on Rails my fantasy: instant array of AR attributes?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
19fe735490c8f7e08d5c2de7dc25c826?d=identicon&s=25 Jason Frankovitz (Guest)
on 2006-12-23 01:58
(Received via mailing list)
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 Frankovitz - jason@seethroo.us - http://www.seethroo.us/
work: (310) 601-8454
cell: (415) 254-4890
AIM/Skype: jfrankov
Efe398b3b5436b0a8d4430090e8f86ea?d=identicon&s=25 seth b. (subimage)
on 2006-12-23 02:04
(Received via mailing list)
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 Frankovitz <jason@seethroo.us> wrote:
>
>
>
--
seth at subimage interactive
http://www.subimage.com/sublog/
Aafa8848c4b764f080b1b31a51eab73d?d=identicon&s=25 Phlip (Guest)
on 2006-12-23 02:12
(Received via mailing list)
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
  http://www.greencheese.us/ZeekLand <-- NOT a blog!!!
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2006-12-23 02:24
(Received via mailing list)
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 (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)
67f61f0c385168c4b2e5b8fd733c397f?d=identicon&s=25 Christos Zisopoulos (Guest)
on 2006-12-23 02:28
(Received via mailing list)
Even shorter:

names = folks.collect(&:name)

-christos
19fe735490c8f7e08d5c2de7dc25c826?d=identicon&s=25 Jason Frankovitz (Guest)
on 2006-12-23 02:54
(Received via mailing list)
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 Frankovitz - jfrankov@pobox.com
work: (310) 601-8454
cell: (415) 254-4890
AIM/Skype: jfrankov
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2006-12-23 03:04
(Received via mailing list)
Hi --

On Fri, 22 Dec 2006, Jason Frankovitz wrote:

> On Dec 22, 2006, at 5:27 PM, Christos Zisopoulos 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 (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)
This topic is locked and can not be replied to.