Class: TextAdventure::Question

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Question

Returns a new instance of Question.



41
42
43
44
# File 'lib/text_adventure.rb', line 41

def initialize(data)
  @question = data['question']
  @answer = data['answers']
end

Instance Attribute Details

#questionObject

Returns the value of attribute question.



39
40
41
# File 'lib/text_adventure.rb', line 39

def question
  @question
end

Instance Method Details

#askObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/text_adventure.rb', line 45

def ask
  begin
    puts @question
    @answer.each do |key,value|
      puts "  (#{key}) #{value['text']}"
    end
    input = gets
    return @answer[input.strip]['key']
  rescue
    puts 'No valid option'
    retry
  end
  return 'death'
end