On 30.10.2006 11:03, Martin DeMello wrote:
> On 10/29/06, Robert Klemme <shortcutter / googlemail.com> wrote:
>> Logan Capaldo wrote:
>> > Alternative decandence:
>> >
>> > class Array
>> >   def to_hsh
>> >     require 'enumerator'
>> >     to_enum(:each_slice, 2).to_a.inject({}) { |h, (k, v)| 
>> h.update(k=>v) }
>> >   end
>> > end
>>
>> This seems a bit inefficient.  If you write a method then I'd prefer
>>
>> require 'enumerator'
>> module Enumerable
>>    def to_hash
>>      h = {}
>>      to_enum(:each_slice, 2).each {|k,v| h[k]=v}
>>      h
>>    end
>> end
> 
> Never thought I'd see you speak out *against* inject :)

Yeah, I must have been out of my mind. :-) I mean, the obvious remedy is 
this:

require 'enumerator'
module Enumerable
   def to_hash
     to_enum(:each_slice, 2).inject({}) {|h, (k,v)| h[k]=v; h}
   end
end

My main point in the other posting was to save the overhead of #to_a and 
all the little two element Hashes.  With regard to /that/ both variants 
are equivalent.  But, yes, you're right of course: I should have posted 
the #inject variant right away.  I apologize.

:-)

Kind regards

	robert