On Tue, Mar 10, 2009 at 4:05 PM, Clifford Heath <[email protected]>wrote:
>
> On 10/03/2009, at 3:52 PM, Nick Partridge wrote:
> > Symbol-to-proc, and the use of `&` to coerce things to procs:
> >
> > [1,2,3,4,5].select(&:odd?)
> >
> > Awww yeah.
>
> Cute but wasteful, and now deprecated in all Rails core code because of
> the extra (non-gc-able?) object it creates.
>
> I like this for creating and adding to hash values consisting of an
> array:
>
> things_by_key = {}
> things.each do |thing|
> (things_by_key[thing.key] ||= []) << thing
> end
>
> or the same using inject...
>
> Clifford Heath.
>
>
Hey Cliff,
One of the things I like with hashes is default values set with procs :)
things_by_key = Hash.new{|h,k| h[k] = []}
things.each do |thing|
things_by_key[thing.key] << thing
end
I use these all the time for default values in all sorts of ways :)
Daniel
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
or Rails Oceania" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/rails-oceania?hl=en
-~----------~----~----~----~------~----~------~--~---