[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: Newbie question: Small blocks

Phlip

9/10/2007 7:38:00 PM

Dante Regis wrote:

> It was something like [0, 1, 2].each { } , with some char inside the
> brackets, like &, but I can't remember it. anyone knows how to use it?

If I guess correctly, ActiveSupport has a cute little tweak that permits
this:

p (0..10).to_a.map(&:to_s)

Any place you can write {|x| x.y}, you can shorten that to (&:y). This works
great with ActiveRecord, to convert a list of database records into a list
of one of its fields:

p User.find(:all).map(&:name)

--
Phlip


1 Answer

Jeremy Woertink

9/10/2007 7:55:00 PM

0

Phlip wrote:
> Dante Regis wrote:
>
>> It was something like [0, 1, 2].each { } , with some char inside the
>> brackets, like &, but I can't remember it. anyone knows how to use it?
>
> If I guess correctly, ActiveSupport has a cute little tweak that permits
> this:
>
> p (0..10).to_a.map(&:to_s)
>
> Any place you can write {|x| x.y}, you can shorten that to (&:y). This
> works
> great with ActiveRecord, to convert a list of database records into a
> list
> of one of its fields:
>
> p User.find(:all).map(&:name)

I totally forgot about that one. I guess I was thinking just Ruby and
not anything with rails. :)


~Jeremy
--
Posted via http://www.ruby-....