Class: Ranker::Rankings

Inherits:
Array
  • Object
show all
Defined in:
lib/ranker/rankings.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(strategy) ⇒ Rankings

Returns a new instance of Rankings.



7
8
9
10
# File 'lib/ranker/rankings.rb', line 7

def initialize(strategy)
  @strategy = strategy
  @scores = []
end

Instance Attribute Details

#scoresObject (readonly)

Returns the value of attribute scores.



5
6
7
# File 'lib/ranker/rankings.rb', line 5

def scores
  @scores
end

#strategyObject (readonly)

Returns the value of attribute strategy.



5
6
7
# File 'lib/ranker/rankings.rb', line 5

def strategy
  @strategy
end

Instance Method Details

#create(rank, score, rankables) ⇒ Object

Methods:



49
50
51
52
# File 'lib/ranker/rankings.rb', line 49

def create(rank, score, rankables)
  scores.concat(Array.new(rankables.count, score))
  self << Ranking.new(self, self.count, rank, score, rankables)
end

#meanObject

Properties:



15
16
17
# File 'lib/ranker/rankings.rb', line 15

def mean
  @mean ||= total.to_f / num_scores
end

#num_scoresObject



28
29
30
# File 'lib/ranker/rankings.rb', line 28

def num_scores
  scores.size
end

#standard_deviationObject



19
20
21
22
23
24
25
26
# File 'lib/ranker/rankings.rb', line 19

def standard_deviation
  @standard_deviation ||= if variance.nan?
    # For ruby-1.8.7 compatibility
    variance
  else
    Math.sqrt(variance)
  end
end

#totalObject



36
37
38
# File 'lib/ranker/rankings.rb', line 36

def total
  @total ||= scores.reduce(:+)
end

#total_differenceObject



40
41
42
43
44
# File 'lib/ranker/rankings.rb', line 40

def total_difference
  @total_difference ||= scores.reduce(0) { |sum, score|
    sum + (score - mean) ** 2
  }
end

#varianceObject



32
33
34
# File 'lib/ranker/rankings.rb', line 32

def variance
  @variance ||= total_difference.to_f / num_scores
end