Skip to content

Instantly share code, notes, and snippets.

@rf-
Created November 30, 2011 06:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rf-/1408283 to your computer and use it in GitHub Desktop.
Save rf-/1408283 to your computer and use it in GitHub Desktop.
def draw_chart(collection, attribute=nil, &block)
if attribute
grouped = collection.group_by(&attribute)
elsif block
grouped = collection.group_by(&block)
else
raise "Must provide either an attribute or block to group by!"
end
# try to sort them, but if they're not sortable, don't worry about it
begin
grouped = grouped.sort_by(&:first)
rescue ArgumentError
end
puts AsciiCharts::Cartesian.new(grouped.map { |k, v| [k, v.count] }).draw
nil
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment