Best way to keep my stats (ruby)

I want to display some statistics of data stored in an array of arrays. I have three categories (video, article, webinar), but it can be expanded later. The structure of statistics by category will be almost the same. For example: the total number of videos, the last date when a new entry in the category was added, etc.

So far, I can provide an hash of the array for storing statistics. If the array contains the statistics structure for each category and is (almost) the same for all categories. Can anyone think of any better solution in terms of

  • easy to include a new category
  • easy to manipulate / assign / calculate all statistics
  • 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
+3
2

Peter : -)

... (, to_s, , )... ( /)...

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
$ 
+3

- ! , .. , Statistics, class ArticleStatistic < Statistics .. , , .

+1

Source: https://habr.com/ru/post/1733816/


All Articles