Class: Calcexam::Exam

Inherits:
Object
  • Object
show all
Defined in:
lib/calcexam/exam.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeExam

Returns a new instance of Exam.



7
8
9
# File 'lib/calcexam/exam.rb', line 7

def initialize
  @answers_count, @right_answers_count = 0, 0
end

Instance Attribute Details

#answers_countObject (readonly)

Returns the value of attribute answers_count.



5
6
7
# File 'lib/calcexam/exam.rb', line 5

def answers_count
  @answers_count
end

#right_answers_countObject (readonly)

Returns the value of attribute right_answers_count.



5
6
7
# File 'lib/calcexam/exam.rb', line 5

def right_answers_count
  @right_answers_count
end

Instance Method Details

#resultsObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/calcexam/exam.rb', line 26

def results
  mark = (right_answers_count.to_f / [answers_count, 1].max * 100).round(1)
  color = case
  when mark >= 90
    :green
  when mark >= 70
    :blue
  when mark >= 50
    :yellow
  else
    :red
  end
  puts "Results: " + "#{mark}%".bright.color(color) + " (#{right_answers_count}/#{answers_count})".color(color)
end

#right_answer!Object



11
12
13
14
15
# File 'lib/calcexam/exam.rb', line 11

def right_answer!
  @answers_count += 1
  @right_answers_count += 1
  puts 'Right!'.bright.color(:green)
end

#try_againObject



22
23
24
# File 'lib/calcexam/exam.rb', line 22

def try_again
  puts 'Try again'.bright.color(:yellow)
end

#wrong_answer!Object



17
18
19
20
# File 'lib/calcexam/exam.rb', line 17

def wrong_answer!
  @answers_count += 1
  puts 'Wrong!'.bright.color(:red)
end