Class: Quiz

Inherits:
Object
  • Object
show all
Defined in:
lib/interval-quiz.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeQuiz

Returns a new instance of Quiz.



31
32
33
34
35
# File 'lib/interval-quiz.rb', line 31

def initialize
  self.asked = 0
  self.correct = 0
  self.natural = true
end

Instance Attribute Details

#abovebelowObject

array of [“”, “-”] if “-” is present, then we are quizzing on intervals below



26
27
28
# File 'lib/interval-quiz.rb', line 26

def abovebelow
  @abovebelow
end

#askedObject

Returns the value of attribute asked.



27
28
29
# File 'lib/interval-quiz.rb', line 27

def asked
  @asked
end

#correctObject

Returns the value of attribute correct.



28
29
30
# File 'lib/interval-quiz.rb', line 28

def correct
  @correct
end

#intervalsObject

Here is the list of intervals by semitone. This is just here for reference:

0 Perfect Unison (P1) Diminished second (dim2) 1 Minor second (m2) Augmented unison (aug1) 2 Major second (M2) Diminished third (dim3) 3 Minor third (m3) Augmented second (aug2) 4 Major third (M3) Diminished fourth (dim4) 5 Perfect fourth (P4) Augmented third (aug3) 6 Tritone Augmented fourth (aug4) Diminished fifth (dim5) 7 Perfect fifth (P5) Diminished sixth (dim6) 8 Minor sixth (m6) Augmented fifth (aug5) 9 Major sixth (M6) Diminished seventh (dim7) 10 Minor seventh (m7) Augmented sixth (aug6) 11 Major seventh (M7) Diminished octave (dim8) 12 Perfect octave (P8) Augmented seventh (aug7)



25
26
27
# File 'lib/interval-quiz.rb', line 25

def intervals
  @intervals
end

#naturalObject

Returns the value of attribute natural.



29
30
31
# File 'lib/interval-quiz.rb', line 29

def natural
  @natural
end

Instance Method Details

#run!Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/interval-quiz.rb', line 37

def run!
  loop do

    pitch = Interval::Pitch.random(self.natural)
    ab = self.abovebelow.rand
    ab_eng = ab == "-" ? "below" : "above"
    interval = Interval::Interval.from_string(ab + self.intervals.rand)

    begin
      answer = ask("what is a #{interval.to_long_name.downcase} #{ab_eng} #{pitch.to_s} #{score_str}? ")
      self.asked = self.asked + 1
    rescue EOFError 
      puts "goodbye"
      exit
    end

    real_answer = pitch + interval

    if answer.downcase == real_answer.to_short_name.downcase
      say "<%= color('correct!', :green) %>"
      self.correct = correct + 1
    else
      say "<%= color('wrong', :red) %>. the answer is #{real_answer.to_short_name}"
    end
  end
end

#score_strObject



64
65
66
67
# File 'lib/interval-quiz.rb', line 64

def score_str
  return "" unless asked && asked > 0
  "#{correct}/#{asked} (%d%%)" % [(correct.to_f / asked.to_f) * 100]
end