class Nametrainer::Statistics

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)

Attributes

correct[RW]
wrong[RW]

Public Class Methods

new() click to toggle source
# File lib/nametrainer/statistics.rb, line 20
def initialize
  reset
end

Public Instance Methods

reset() click to toggle source

Resets all values to zero.

# File lib/nametrainer/statistics.rb, line 25
def reset
  @correct = 0
  @wrong = 0
end
to_s() click to toggle source

Returns a string with percentage and correct and total answers.

# 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() click to toggle source

Returns the total number of answers.

# File lib/nametrainer/statistics.rb, line 31
def total
  @correct + @wrong
end