Created
November 30, 2011 06:50
-
-
Save rf-/1408283 to your computer and use it in GitHub Desktop.
This file contains 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
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