Class: Ranker::Rankings
- Inherits:
-
Array
- Object
- Array
- Ranker::Rankings
- Defined in:
- lib/ranker/rankings.rb
Instance Attribute Summary collapse
-
#scores ⇒ Object
readonly
Returns the value of attribute scores.
-
#strategy ⇒ Object
readonly
Returns the value of attribute strategy.
Instance Method Summary collapse
-
#create(rank, score, rankables) ⇒ Object
Methods:.
-
#initialize(strategy) ⇒ Rankings
constructor
A new instance of Rankings.
-
#mean ⇒ Object
Properties:.
- #num_scores ⇒ Object
- #standard_deviation ⇒ Object
- #total ⇒ Object
- #total_difference ⇒ Object
- #variance ⇒ Object
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
#scores ⇒ Object (readonly)
Returns the value of attribute scores.
5 6 7 |
# File 'lib/ranker/rankings.rb', line 5 def scores @scores end |
#strategy ⇒ Object (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 |
#mean ⇒ Object
Properties:
15 16 17 |
# File 'lib/ranker/rankings.rb', line 15 def mean @mean ||= total.to_f / num_scores end |
#num_scores ⇒ Object
28 29 30 |
# File 'lib/ranker/rankings.rb', line 28 def num_scores scores.size end |
#standard_deviation ⇒ Object
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 |
#total ⇒ Object
36 37 38 |
# File 'lib/ranker/rankings.rb', line 36 def total @total ||= scores.reduce(:+) end |
#total_difference ⇒ Object
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 |
#variance ⇒ Object
32 33 34 |
# File 'lib/ranker/rankings.rb', line 32 def variance @variance ||= total_difference.to_f / num_scores end |