Created
May 25, 2015 08:56
-
-
Save iliabylich/9ea5dd31ee92571f8d59 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Survey | |
def initialize(title, &block) | |
@title = title | |
instance_eval(&block) | |
end | |
def question(question_title, &block) | |
questions << Question.new(question_title, &block) | |
end | |
def questions | |
@questions ||= [] | |
end | |
def run | |
puts @title | |
questions.each(&:run) | |
end | |
end | |
class Question | |
def initialize(title, &block) | |
@title = title | |
instance_eval(&block) | |
end | |
def answer(answer_text) | |
answers << answer_text | |
end | |
def answers | |
@answers ||= [] | |
end | |
def run | |
puts @title | |
answers.each_with_index do |answer, index| | |
puts "#{index + 1}. #{answer}" | |
end | |
result_number = gets.chomp.to_i - 1 | |
result = answers[result_number] | |
puts "Your answer is #{result}" | |
end | |
end | |
def survey(title, &block) | |
Survey.new(title, &block).run | |
end | |
survey 'Survey title' do | |
question 'Question 1 title' do | |
answer 'Option 1' | |
answer "Option 2" | |
end | |
question 'Question 2 title' do | |
answer 'Option 3' | |
answer 'Option 4' | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment