Class: Quiz

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, &block) ⇒ Quiz

Returns a new instance of Quiz.



7
8
9
10
11
12
13
# File 'lib/Exam/quiz.rb', line 7

def initialize(name, &block)
	@name = name
	@exam = Exam.new(Question.new(:qt => "-", :r1 => "-", :wrong => ["-"]))
	@exam.exam.pop
	@counter = 0
	instance_eval &block
end

Instance Attribute Details

#counterObject

Returns the value of attribute counter.



5
6
7
# File 'lib/Exam/quiz.rb', line 5

def counter
  @counter
end

#examObject

Returns the value of attribute exam.



5
6
7
# File 'lib/Exam/quiz.rb', line 5

def exam
  @exam
end

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/Exam/quiz.rb', line 5

def name
  @name
end

Instance Method Details

#question(*args) ⇒ Object



24
25
26
27
28
29
# File 'lib/Exam/quiz.rb', line 24

def question(*args)
	wrong = Array.new
	args[1].keys.each { |x| wrong << args[1][x] if x.instance_of?(Array)}
	@exam.push(Question.new(:qt => args[0], :r1 => args[1][:right], :wrong => wrong))
	@exam
end

#rightObject



15
16
17
# File 'lib/Exam/quiz.rb', line 15

def right
	:right
end

#runObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/Exam/quiz.rb', line 35

def run
	puts "#{@name}\n\n"
	punt = 0
	right = @exam.right
	userans = Array.new(right.count, false)
	@exam.exam.each_with_index do |x, i|
		 puts x
		 print "Su respuesta es: "
		 STDOUT.flush
		 if right[i].to_s.eql?gets.chomp
		 		punt += 1
		 end
		 puts "\n"
	end
	puts "Ha acertado #{punt} de #{right.count}" 
end

#to_sObject



31
32
33
# File 'lib/Exam/quiz.rb', line 31

def to_s
	"\t#{@name}\n\n#{@exam}"
end

#wrongObject



19
20
21
22
# File 'lib/Exam/quiz.rb', line 19

def wrong
	@counter += 1
	[:wrong, @counter]
end