Created
April 26, 2012 20:55
-
-
Save zuchmanski/2503121 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#sebcioz: programs is a hash containing rows from a mysql query with two fields: program_id and program_name. | |
#participants is a hash containing rows from a mysql query with several fields, one of which is program_id. I would like to create a third hash that puts participants into groups based on program_id | |
programs = [{:program_id => 1, :program_name => :a}, {:program_id => 2, :program_name => :b}, {:program_id => 3, :program_name => :c}] | |
participants = [{:name => :q, :program_id => 1}, {:name => :w, :program_id => 1}, {:name => :e, :program_id => 2}] | |
groups = participants.group_by { |p| p[:program_id] } | |
p groups #=> {1=>[{:name=>:q, :program_id=>1}, {:name=>:w, :program_id=>1}], 2=>[{:name=>:e, :program_id=>2}]} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment