Skip to content

Instantly share code, notes, and snippets.

Created August 4, 2013 02:12

Revisions

  1. @invalid-email-address Anonymous created this gist Aug 4, 2013.
    50 changes: 50 additions & 0 deletions bob.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,50 @@
    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