2008/1/9, Holden Holden <pyarnau / gmail.com>:
> I want to convert an array (with pairs [key,value]) to a hash. After
> googling a bit I got:
>
> 1) Hash[*array.flatten]
>
> But this is not safe as Array#flatten is recursive.
>
> 2) array.inject({}) { |m, e| m[e[0]] = e[1]; m }
>
> Too ugly to consider.

You can do this:

array.inject({}) {|ha, (k, v)| ha[k] = v; ha}

Is this nicer according to your standards?

Kind regards

robert


-- 
use.inject do |as, often| as.you_can - without end