Skip to content

Instantly share code, notes, and snippets.

@radar
Last active September 19, 2018 01:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save radar/26a95a7e9b6dfc2558af6e2b6dea27bd to your computer and use it in GitHub Desktop.
Save radar/26a95a7e9b6dfc2558af6e2b6dea27bd to your computer and use it in GitHub Desktop.
class Question
end
class Answer
attr_reader :question, :score
def initialize(question, score)
@question = question
@score = score
end
end
module AnswerScoreHelpers
refine Answer do
def low?
score < 3
end
def neutral?
score == 3
end
def high?
score > 3
end
end
end
class Response
using AnswerScoreHelpers
attr_reader :answers
def initialize(answers = [])
@answers = answers
end
def low_answers_for_question(question)
answers.select(&for_question(question)).select(&:low?)
end
def for_question(question)
->(answer) { answer.question == question }
end
end
question = Question.new
answer_1 = Answer.new(question, 1)
answer_2 = Answer.new(question, 2)
answer_3 = Answer.new(question, 3)
answer_4 = Answer.new(question, 4)
answer_5 = Answer.new(question, 5)
response = Response.new([answer_1, answer_2, answer_3, answer_4, answer_5])
p response.low_answers_for_question(question)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment