quiz 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/quiz.rb +19 -12
  2. metadata +2 -2
@@ -15,28 +15,35 @@ class Quiz
15
15
  def start(n=3)
16
16
 
17
17
  nodes = XPath.match(@doc.root, '*[question]')
18
- list = (0..nodes.length).to_a.sample(n)
18
+ list = (0...nodes.length).to_a.sample(n)
19
19
  questions = nodes.values_at(*list)
20
20
 
21
- results = questions.map do |q|
21
+ @results = questions.map do |q|
22
22
  question, answer = q.text('question').strip, q.text('answer').strip
23
23
  *options = XPath.match(q,'./options//option/text()').sort{rand}
24
24
  a_options = options.length.times.map {|x| (97 + x).chr }.\
25
25
  zip(options.map {|x| x.to_s.strip})
26
26
  q_options = a_options.map {|x| " " * 2 + "%s) %s" % x}
27
- h = Hash[a_options.to_a] # answers are in the hash, keys = a,b,c,d
28
- puts question + "\nis it ... \n" + q_options.join("\n")
27
+ answers = Hash[a_options.to_a] # answers are in the hash, keys = a,b,c,d
29
28
 
30
- student_answer = gets.chop; redo unless h.keys.include? student_answer
31
-
32
- result = h[student_answer] == answer
33
- reply = result ? '* correct *' : 'the answer was ' + answer
34
- puts reply
29
+ if block_given? then
30
+ result = yield(question, q_options, answers, answer)
31
+ else
32
+ puts question + "\nis it ... \n" + q_options.join("\n")
33
+
34
+ student_answer = gets.chop; redo unless answers.keys.include? student_answer
35
+
36
+ result = answers[student_answer] == answer
37
+ reply = result ? '* correct *' : 'the answer was ' + answer
38
+ puts reply
39
+ end
35
40
  result
36
41
  end
37
-
38
- tally = (results - [false]).size
39
- percent = 100 / (results.size / tally.to_f)
42
+ end
43
+
44
+ def score()
45
+ tally = (@results - [false]).size
46
+ percent = 100 / (@results.size / tally.to_f)
40
47
  your_score = "you scored %s%" % percent.to_i
41
48
 
42
49
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: quiz
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.0
5
+ version: 0.1.1
6
6
  platform: ruby
7
7
  authors:
8
8
  - James Robertson
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2012-02-09 00:00:00 +00:00
13
+ date: 2012-02-10 00:00:00 +00:00
14
14
  default_executable:
15
15
  dependencies: []
16
16