James Gray
12/19/2006 6:44:00 PM
On Dec 19, 2006, at 10:30 AM, Brad Phelan wrote:
> Woga Swoga wrote:
>> I have an list of objects (Candy) and each object has two fields
>> (Candy.color, Candy.taste). I would like to display the candies but
>> group them based on the fields (display a header for each group).
>> The
>> problem is I don't know how many colors or tastes are in any
>> particular
>> list of candies.
>> This is what I have for input:
>> * Candy <-- hash list already pre-sorted by "group"
>> * group <-- variable contains either "color" or "taste"
>> 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!
>
> require 'yaml'
>
> @candies = [
> { :name => 'a', :color => "blue", :taste => "good" },
> { :name => 'b',:color => "blue", :taste => "yuck" },
> { :name => 'c',:color => "red", :taste => "ok" },
> { :name => 'd',:color => "blue", :taste => "yuck" },
> { :name => 'e',:color => "green", :taste => "good" },
> { :name => 'f',:color => "green", :taste => "yuck" }
> ]
>
> def print_candies_by(group)
> groups={}
> @candies.each do |candy|
> key = candy[group]
> groups[key] = [] unless groups[key]
> groups[key] << candy
> end
>
> puts "##########'"
> puts YAML.dump groups
> puts "##########'"
> end
>
>
> print_candies_by :taste
> print_candies_by :color
You can also use sets for this:
>> require "set"
=> true
>> candies = [
?> { :name => 'a', :color => "blue", :taste => "good" },
?> { :name => 'b',:color => "blue", :taste => "yuck" },
?> { :name => 'c',:color => "red", :taste => "ok" },
?> { :name => 'd',:color => "blue", :taste => "yuck" },
?> { :name => 'e',:color => "green", :taste => "good" },
?> { :name => 'f',:color => "green", :taste => "yuck" }
>> ]
=> [{:color=>"blue", :taste=>"good", :name=>"a"},
{:color=>"blue", :taste=>"yuck", :name=>"b"},
{:color=>"red", :taste=>"ok", :name=>"c"},
{:color=>"blue", :taste=>"yuck", :name=>"d"},
{:color=>"green", :taste=>"good", :name=>"e"},
{:color=>"green", :taste=>"yuck", :name=>"f"}]
>> candies.to_set.classify { |c| c[:color] }
=> {"green"=>#<Set: {{:color=>"green", :taste=>"yuck", :name=>"f"},
{:color=>"green", :taste=>"good", :name=>"e"}}>, "blue"=>#<Set:
{{:color=>"blue", :taste=>"good", :name=>"a"},
{:color=>"blue", :taste=>"yuck", :name=>"d"},
{:color=>"blue", :taste=>"yuck", :name=>"b"}}>, "red"=>#<Set:
{{:color=>"red", :taste=>"ok", :name=>"c"}}>}
James Edward Gray II