quiz 0.1.0

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 +43 -0
  2. metadata +56 -0
data/lib/quiz.rb ADDED
@@ -0,0 +1,43 @@
1
+ #!/usr/bin/ruby
2
+
3
+ # file: quiz.rb
4
+
5
+ require 'rxfhelper'
6
+ require 'rexml/document'
7
+
8
+ class Quiz
9
+ include REXML
10
+
11
+ def initialize(x)
12
+ @doc = Document.new(RXFHelper.read(x))
13
+ end
14
+
15
+ def start(n=3)
16
+
17
+ nodes = XPath.match(@doc.root, '*[question]')
18
+ list = (0..nodes.length).to_a.sample(n)
19
+ questions = nodes.values_at(*list)
20
+
21
+ results = questions.map do |q|
22
+ question, answer = q.text('question').strip, q.text('answer').strip
23
+ *options = XPath.match(q,'./options//option/text()').sort{rand}
24
+ a_options = options.length.times.map {|x| (97 + x).chr }.\
25
+ zip(options.map {|x| x.to_s.strip})
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")
29
+
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
35
+ result
36
+ end
37
+
38
+ tally = (results - [false]).size
39
+ percent = 100 / (results.size / tally.to_f)
40
+ your_score = "you scored %s%" % percent.to_i
41
+
42
+ end
43
+ end
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: quiz
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: ruby
7
+ authors:
8
+ - James Robertson
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2012-02-09 00:00:00 +00:00
14
+ default_executable:
15
+ dependencies: []
16
+
17
+ description:
18
+ email:
19
+ executables: []
20
+
21
+ extensions: []
22
+
23
+ extra_rdoc_files: []
24
+
25
+ files:
26
+ - lib/quiz.rb
27
+ has_rdoc: true
28
+ homepage:
29
+ licenses: []
30
+
31
+ post_install_message:
32
+ rdoc_options: []
33
+
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: "0"
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: "0"
48
+ requirements: []
49
+
50
+ rubyforge_project:
51
+ rubygems_version: 1.5.2
52
+ signing_key:
53
+ specification_version: 3
54
+ summary: quiz
55
+ test_files: []
56
+