Skip to content

Instantly share code, notes, and snippets.

@theotherzach
Created August 3, 2013 19:53
Show Gist options
  • Select an option

  • Save theotherzach/6147757 to your computer and use it in GitHub Desktop.

Select an option

Save theotherzach/6147757 to your computer and use it in GitHub Desktop.
## Example 3 ###
class Charlie
def hey(drivel)
answerer(drivel).reply
end
private
def answerer(drivel)
handlers.find {|answer| answer.handles?(drivel)}.new
end
def handlers
[AnswerSilence, AnswerShout, AnswerQuestion, AnswerDefault]
end
end
class AnswerSilence
def self.handles?(input)
input.nil? || input.empty?
end
def reply
'Fine. Be that way!'
end
end
class AnswerQuestion
def self.handles?(input)
input.end_with?('?')
end
def reply
'Sure.'
end
end
class AnswerShout
def self.handles?(input)
input == input.upcase
end
def reply
'Woah, chill out!'
end
end
class AnswerDefault
def self.handles?(input)
true
end
def reply
'Whatever.'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment