From: Drew Olson Date: 2006-12-20T22:51:55+09:00 Subject: Re: ruby puzzle: Group candies by unique record fields Woga Swoga wrote: > Can anyone come up with a ruby code snipet that will use the "group" > variable to extract out the group labels (unique values of the color or > tate field), and then dump out the Candy list by group. > > Really appreciate your help! Not sure if your Candy list is actually a hash or just an array of Candy objects. I'll write the code assuming the later. group_flag = false group_flag = true if group == "color" grouping = {} candy.each do |piece| element = piece.color if group_flag element = piece.taste if !group_flag grouping[element] ||= [] grouping[element] << piece end grouping.each do |key,element| puts "Grouping: #{key}" element.each do |piece| puts " "+candy.name end end -- Posted via http://www.ruby-forum.com/.