Class: Quiz
- Inherits:
-
Object
- Object
- Quiz
- Defined in:
- lib/Examenes/quiz.rb
Instance Attribute Summary collapse
-
#answers ⇒ Object
Returns the value of attribute answers.
-
#name ⇒ Object
Returns the value of attribute name.
-
#questions ⇒ Object
Returns the value of attribute questions.
Instance Method Summary collapse
-
#initialize(name, &block) ⇒ Quiz
constructor
A new instance of Quiz.
- #question(name, options = {}) ⇒ Object
- #run(eleccion) ⇒ Object
- #to_s ⇒ Object
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
#answers ⇒ Object
Returns the value of attribute answers.
3 4 5 |
# File 'lib/Examenes/quiz.rb', line 3 def answers @answers end |
#name ⇒ Object
Returns the value of attribute name.
3 4 5 |
# File 'lib/Examenes/quiz.rb', line 3 def name @name end |
#questions ⇒ Object
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, = {}) @counter = 0 q = name answers << {} answers[@i][:wrong] = [] if [:right] answers[@i][:right] = [:right] q << "\n\t#{@counter+=1}) #{[:right]}" end if [:wrong] answers[@i][:wrong] << [:wrong] q << "\n\t#{@counter+=1}) #{[: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_s ⇒ Object
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 |