Skip to content

Instantly share code, notes, and snippets.

@zuchmanski
Created April 26, 2012 20:55
Show Gist options
  • Save zuchmanski/2503121 to your computer and use it in GitHub Desktop.
Save zuchmanski/2503121 to your computer and use it in GitHub Desktop.
#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