From: Josh Cheek Date: 2010-03-19T12:48:30+09:00 Subject: Re: Grouping elements of an array --000e0cd1ab8065bd6504821f3865 Content-Type: text/plain; charset=ISO-8859-1 On Thu, Mar 18, 2010 at 9:29 PM, Urabe Shyouhei wrote: > Roger Braun wrote: > > How about this: > > You should really know about Enumerable#group_by. > > irb(main):001:0> [0,15,35,100,205,300].group_by {|i| i/100 } > => {0=>[0, 15, 35], 1=>[100], 2=>[205], 3=>[300]} > > > Your results are correct only because of a happenstance of the data. Add 99 in there, it should group with 100, but it groups with the 0...100 congruence class ruby-1.9.1-p378 > [0,15,35,99,100,205,300].group_by {|i| i/100 } => {0=>[0, 15, 35, 99], 1=>[100], 2=>[205], 3=>[300]} Because these groups are relative to each other, I think you must do something like Roger or I did, where you iterate through the list and compare it to the groups. --000e0cd1ab8065bd6504821f3865--