Skip to content

Instantly share code, notes, and snippets.

@iliabylich
Created May 25, 2015 08:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iliabylich/9ea5dd31ee92571f8d59 to your computer and use it in GitHub Desktop.
Save iliabylich/9ea5dd31ee92571f8d59 to your computer and use it in GitHub Desktop.
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