Skip to content

Instantly share code, notes, and snippets.

Created August 4, 2013 02:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/6148795 to your computer and use it in GitHub Desktop.
Save anonymous/6148795 to your computer and use it in GitHub Desktop.
Bob without if's and case
class Bob
def hey(phrase)
think_about(heard(phrase)).respond
end
def heard(phrase)
String(phrase)
end
def think_about(might_have_heard)
THOUGHTS.find(-> { Default }) { |thought| thought.heard?(might_have_heard) }
end
class << Silent = Object.new
def heard?(other)
other.empty?
end
def respond
"Fine. Be that way."
end
end
class << Question = Object.new
def heard?(other)
other.end_with?("?")
end
def respond
"Sure."
end
end
class << Shout = Object.new
def heard?(other)
other == other.upcase
end
def respond
"Woah, chill out!"
end
end
class << Default = Object.new
def respond
"Whatever."
end
end
THOUGHTS = [Silent, Question, Shout]
end
@adkron
Copy link

adkron commented Aug 4, 2013

Oops, I apparently wasn't logged in when I posted this.

@adkron
Copy link

adkron commented Aug 8, 2013

please comment on this one instead. https://gist.github.com/adkron/6148836

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment