From: Robert Klemme Date: 2005-10-25T16:22:03+09:00 Subject: Re: Array#to_h Daniel Sheppard wrote: > Problem is - which [].to_h is intuitive. There's a to_h in nano: > > http://nano.rubyforge.org/doc/classes/Array.html#M000057 > > You can already do: > > a = [1, 2, 3, 4, 5] > hash = a.inject({}) {|h,x| h[x] = x*2} Won't work. This at least has to be hash = a.inject({}) {|h,x| h[x] = x*2;h} > Or: > > class Array > def to_h > a.inject({}) { |h,x| h[x] = yield(x) } Same here: a.inject({}) { |h,x| h[x] = yield(x);h } > end > end > hash = a.to_h { |x| x*2} > > I think the inject method most clearly shows what's going on. Kind regards robert