On 10/03/2009, at 4:15 PM, Daniel N wrote:
> 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 :)
I don't prefer that. The syntax requires more knowledge, and
the result isn't faster (is it? possibly it avoids two hash() calls?).
Also the fact that the default isn't present where you add entries
tends to engender the mistake of direct assignment instead of
the correct initialize sequence.
Possibly if you defined:
class Hash
def self.new_to_array
new{|h,k| h[k] = [] }
end
end
so you can say
> things_by_key = Hash.new_to_array
> things.each do |thing|
> things_by_key[thing.key] << thing
> end
... I'd be less fussed by those differences.
Clifford Heath.
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---