Class: Quiz

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(titulo, &block) ⇒ Quiz

Returns a new instance of Quiz.



3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/simpleselect/quiz.rb', line 3

def initialize(titulo, &block)
   @titulo=titulo
   self.questions=[]
   self.answers=[]
   
   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.



2
3
4
# File 'lib/simpleselect/quiz.rb', line 2

def answers
  @answers
end

#questionsObject

Returns the value of attribute questions.



2
3
4
# File 'lib/simpleselect/quiz.rb', line 2

def questions
  @questions
end

#tituloObject

Returns the value of attribute titulo.



2
3
4
# File 'lib/simpleselect/quiz.rb', line 2

def titulo
  @titulo
end

Instance Method Details

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



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

def question(name, options = {})
  q = name
  aux = []
  if options[:right] == nil
     puts "No hay una respuesta correcta"
      return
  end
  if options[:wrong] == nil
      puts "No hay ninguna respuesta incorrecta"
      return
  end
  
  aux << " *) #{options[:right]}" 
  options[:wrong].each do |w|
      aux << " *) #{w}"  
  end
  questions << q
  answers << aux
end

#to_sObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/simpleselect/quiz.rb', line 17

def to_s
   output = @titulo
   output << "\n#{'=' * titulo.size}\n"
   
   questions.each_with_index do |q, ind|
       output << "\n"
       output << q
       output <<"\n"
       
       answers[ind].each do |an|
           output << an
           output << "\n"
       end
   end
   output
end