Instantly share code, notes, and snippets.

Embed
What would you like to do?
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