views:

116

answers:

2

I want to display some statistics of data stored in array of arrays. I have three categories (video,article,webinar) but it could expand later on. The structure of statistics per category will be almost the same. Like: total number of videos, last date when added new record in category, etc.

So far I can think of a hash of an array to store the stats. Where the array would hold the structure of stats per category and would be the (almost) same for all categories. Could anybody think of any better solution in terms of

  • being easy to include new category
  • easy to manipulate/assign/calculate all the statistical data
  • easy to display

my idea looks like

stats = { 'video' = [], 'article' = [], 'webinar' = [] } 
stats_array = ['Total number','Last date added','etc']

and then I would do something like

stats['video'][stats_array.index('Total number')] +=1
+1  A: 

Use an object oriented solution! It has little coding overhead, but allows for easy expansion etc. Perhaps create a Statistics class, then a class ArticleStatistic < Statistics class etc. You might not need all that power early, but it's cleaner and more expandable.

Peter
+3  A: 

I voted for Peter's answer :-)

Here is an example ... (updated to have a to_s rather than a print helper, which didn't get pasted in anyway) ... (updated again for sorting/array question) ...

class Stats
  attr_accessor :type, :count, :last_date;
  def initialize t
    @type = t
  end
  def to_s
    "%-9s %4d %s" % [@type, @count, @last_date]
  end
  def <=> other
    [@type, @last_date, @count] <=> [other.type, other.count, other.last_date]
  end
end

all = []
v = Stats.new 'video'
v.count = 12
v.last_date = 'Tuesday'
all << v
a = Stats.new 'article'
a.count = 5
a.last_date = 'Monday'
all << a

puts v
puts a
puts "Ask, and ye shall be sorted..."
puts all.sort
$ ruby r5.rb
video       12 Tuesday
article      5 Monday
Ask, and ye shall be sorted...
article      5 Monday
video       12 Tuesday
$ 
DigitalRoss
@DigitalRoss: maybe I don't use object because I didn't understand your example ...
Radek
Ok, so, your Array's and Hashes are just classes that come with the core system. So the idea is that your stats are complex enough to deserve their own class. This class will contain as instance variables previously defined types, such as your array. But rather than compose array elements with magic meanings, we can just give names to the various things you are going to keep track of; we can even use arrays as instance variables if they make sense. I'll try to hack together a more complete example.
DigitalRoss
Ok, more complete example given...
DigitalRoss
I voted for yours, for taking the time to write such a detailed example :)
Peter
OK, I understand now ... Thank you for great example.
Radek
So let's say I have 700 instances of Stats class. I want to sort all off them by type, last_date and the count. I know how to do it easily using arrays but objects?
Radek
@DigitalRoss: thank you for the update ...
Radek
@Radek, np. We do want to use Array when it makes sense, so if you have 700 stat objects by all means make an Array of them. An Array can be sorted if the individual objects define a spaceship operator, so I've added such a def to the object.
DigitalRoss