Class: Ranker::Rankings
- Inherits:
-
Array
- Object
- Array
- Ranker::Rankings
- Defined in:
- lib/ranker/rankings.rb
Defined Under Namespace
Classes: Error
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_ranking(rank, score, rankables) ⇒ Object
Methods:.
-
#errors ⇒ Object
Properties:.
-
#initialize(strategy) ⇒ Rankings
constructor
A new instance of Rankings.
- #mean ⇒ Object
- #num_scores ⇒ Object
- #standard_deviation ⇒ Object
- #total ⇒ Object
- #total_difference ⇒ Object
- #valid? ⇒ Boolean
- #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_ranking(rank, score, rankables) ⇒ Object
Methods:
60 61 62 63 64 65 |
# File 'lib/ranker/rankings.rb', line 60 def create_ranking(rank, score, rankables) scores.concat(Array.new(rankables.count, score)) ranking = Ranking.new(self, self.count, rank, score, rankables) self << ranking ranking end |
#errors ⇒ Object
Properties:
15 16 17 |
# File 'lib/ranker/rankings.rb', line 15 def errors @errors ||= {} end |
#mean ⇒ Object
19 20 21 |
# File 'lib/ranker/rankings.rb', line 19 def mean @mean ||= total.to_f / num_scores end |
#num_scores ⇒ Object
32 33 34 |
# File 'lib/ranker/rankings.rb', line 32 def num_scores scores.size end |
#standard_deviation ⇒ Object
23 24 25 26 27 28 29 30 |
# File 'lib/ranker/rankings.rb', line 23 def standard_deviation @standard_deviation ||= if variance.nan? # For ruby-1.8.7 compatibility variance else Math.sqrt(variance) end end |
#total ⇒ Object
40 41 42 43 44 |
# File 'lib/ranker/rankings.rb', line 40 def total raise Error.new(errors) unless valid? @total ||= scores.reduce(:+) end |
#total_difference ⇒ Object
46 47 48 49 50 |
# File 'lib/ranker/rankings.rb', line 46 def total_difference @total_difference ||= scores.reduce(0) { |sum, score| sum + (score - mean) ** 2 } end |
#valid? ⇒ Boolean
52 53 54 55 |
# File 'lib/ranker/rankings.rb', line 52 def valid? validate errors.empty? end |
#variance ⇒ Object
36 37 38 |
# File 'lib/ranker/rankings.rb', line 36 def variance @variance ||= total_difference.to_f / num_scores end |