Skip to content

Instantly share code, notes, and snippets.

@kyledecot
Created December 11, 2013 14:51
Show Gist options
  • Save kyledecot/7911708 to your computer and use it in GitHub Desktop.
Save kyledecot/7911708 to your computer and use it in GitHub Desktop.
# Given a CSV of people, display them grouped together by their favorite colors
require 'csv'
# Gather the data...
results = Array.new.tap do |a|
CSV.foreach File.expand_path('../people.csv', __FILE__), headers: true do |row|
a << row.to_hash
end
end.reject do |person|
person['Favorite Color'].nil?
end.group_by do |person|
person['Favorite Color']
end
# Display it in some kind of useful way...
results.each do |color, people|
puts color
puts "==============="
people.each do |person|
puts "#{person['First Name']} #{person['Last Name']}"
end
puts "\n"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment