Class: Quiz

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, &block) ⇒ Quiz

Returns a new instance of Quiz.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/Examenes/quiz.rb', line 5

def initialize name, &block
            @name = name
            @questions = []
            @answers = []
            @i = 0
            @counter = 0

    if block_given?
        if block.arity == 1
            yield self
        else
            instance_eval &block
        end
    end
end

Instance Attribute Details

#answersObject

Returns the value of attribute answers.



3
4
5
# File 'lib/Examenes/quiz.rb', line 3

def answers
  @answers
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/Examenes/quiz.rb', line 3

def name
  @name
end

#questionsObject

Returns the value of attribute questions.



3
4
5
# File 'lib/Examenes/quiz.rb', line 3

def questions
  @questions
end

Instance Method Details

#question(name, options = {}) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/Examenes/quiz.rb', line 30

def question(name, options = {})
    @counter = 0
    q = name
    answers << {}
    answers[@i][:wrong] = []

    if options[:right]
        answers[@i][:right] = options[:right]
        q << "\n\t#{@counter+=1}) #{options[:right]}"
    end

    if options[:wrong]
        answers[@i][:wrong] << options[:wrong]
        q << "\n\t#{@counter+=1}) #{options[:wrong]}"
    end
    
    questions << q
    @i += 1
end

#run(eleccion) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/Examenes/quiz.rb', line 50

def run eleccion
    cor = ""
    
    cor << "#{name}\n#{'=' * name.size}\n\n"
    questions.each_with_index do |question, index|
        cor << "#{index+1}) #{question}\n"
        cor << "Respuesta: #{eleccion[index]}\n"
    end
    contador = 0

    eleccion.each_with_index do |x,i|
        if (x == answers[i][:right])
            cor << "Pregunta #{i+1} correcta.\n"
            contador+=1
        else
            cor << "Pregunta #{i+1} incorrecta.\n"
        end
    end
        if contador == eleccion.size
            cor << "Nota: Sobresaliente.\n"
        elsif contador >= eleccion.size/2
            cor << "Nota: Aprobado.\n"
        else
            cor << "Nota: Suspendido.\n"
        end
        cor
end

#to_sObject



21
22
23
24
25
26
27
28
# File 'lib/Examenes/quiz.rb', line 21

def to_s
    output = name
    output << "\n#{'=' * name.size}\n\n"
    questions.each_with_index do |question, index|
        output << "#{index+1}) #{question}\n"
    end
    output
end