Class: Nametrainer::Statistics

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initializeStatistics

Returns a new instance of Statistics.



20
21
22
# File 'lib/nametrainer/statistics.rb', line 20

def initialize
  reset
end

Instance Attribute Details

#correctObject

Returns the value of attribute correct.



18
19
20
# File 'lib/nametrainer/statistics.rb', line 18

def correct
  @correct
end

#wrongObject

Returns the value of attribute wrong.



18
19
20
# File 'lib/nametrainer/statistics.rb', line 18

def wrong
  @wrong
end

Instance Method Details

#resetObject

Resets all values to zero.



25
26
27
28
# File 'lib/nametrainer/statistics.rb', line 25

def reset
  @correct = 0
  @wrong = 0
end

#to_sObject

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

#totalObject

Returns the total number of answers.



31
32
33
# File 'lib/nametrainer/statistics.rb', line 31

def total
  @correct + @wrong
end