From: Michal Rokos Date: 2002-01-07T01:56:14+09:00 Subject: [ruby-talk:30352] Re: OT: Re: Sorting a Hash by value of integer stored in the Hash Hello, Massimiliano Mirra wrote: > On Sun, Jan 06, 2002 at 09:15:56PM +0900, Michal Rokos wrote: >> > Hash[ *ary.flatten ] >> OK, this works, but let's imagine another ex.: >> a = [[1,[2,3]],[4,[5,6]]] >> I want {1=>[2,3], 4=>[5,6]}, not {1=>2, 3=>4, 5=>6}... :-) > > class Array > # Flatten n levels (one by default) > # > def flatten2(level = 1) > flattened = [] > each do |item| > if item.kind_of?(Array) > flattened.concat item > else > flattened.push item > end > end > flattened = flattened.flatten2(level-1) if level > 1 > return flattened > end > end Good. I really think that we need symmetric function for ary.to_h in official distribution! This is maybe good way (above). Once again: h = {1=>[2,3],4=>[5,6]} a = h.to_a # gives [1,[2,3]],[4,[5,6]]] a.to_h # should give h! - and this isn't now Does anybody feels it the same? Michal -- -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- Michal Rokos Czech Technical University, Prague E-mail: m.rokos@sh.cvut.cz Jabber: majkl@jabber.cz, ICQ: 36118339 -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-