Class: Nametrainer::Statistics
- Inherits:
-
Object
- Object
- Nametrainer::Statistics
- Defined in:
- lib/nametrainer/statistics.rb
Overview
A class for statistics.
It keeps track of the number of correct and wrong answers.
Create an instance with
stats = Statistics.new
Set the values
stats.correct = 6
stats.wrong = 2
Print the percentage
puts stats.to_s => 75 % (6/8)
Instance Attribute Summary collapse
-
#correct ⇒ Object
Returns the value of attribute correct.
-
#wrong ⇒ Object
Returns the value of attribute wrong.
Instance Method Summary collapse
-
#initialize ⇒ Statistics
constructor
A new instance of Statistics.
-
#reset ⇒ Object
Resets all values to zero.
-
#to_s ⇒ Object
Returns a string with percentage and correct and total answers.
-
#total ⇒ Object
Returns the total number of answers.
Constructor Details
#initialize ⇒ Statistics
Returns a new instance of Statistics.
20 21 22 |
# File 'lib/nametrainer/statistics.rb', line 20 def initialize reset end |
Instance Attribute Details
#correct ⇒ Object
Returns the value of attribute correct.
18 19 20 |
# File 'lib/nametrainer/statistics.rb', line 18 def correct @correct end |
#wrong ⇒ Object
Returns the value of attribute wrong.
18 19 20 |
# File 'lib/nametrainer/statistics.rb', line 18 def wrong @wrong end |
Instance Method Details
#reset ⇒ Object
Resets all values to zero.
25 26 27 28 |
# File 'lib/nametrainer/statistics.rb', line 25 def reset @correct = 0 @wrong = 0 end |
#to_s ⇒ Object
Returns a string with percentage and correct and total answers.
36 37 38 39 40 |
# File 'lib/nametrainer/statistics.rb', line 36 def to_s percent = (total == 0) ? 0 : (@correct.to_f / total.to_f * 100).to_i "#{percent} % (#{@correct}/#{total})" end |
#total ⇒ Object
Returns the total number of answers.
31 32 33 |
# File 'lib/nametrainer/statistics.rb', line 31 def total @correct + @wrong end |