Skip to content

Instantly share code, notes, and snippets.

@tkiefhaber
Last active December 26, 2015 13:59
Show Gist options
  • Save tkiefhaber/7161765 to your computer and use it in GitHub Desktop.
Save tkiefhaber/7161765 to your computer and use it in GitHub Desktop.
# Bob
Bob is a lackadaisical teenager. In conversation, his responses are very limited.
Bob answers 'Sure.' if you ask him a question.
He answers 'Woah, chill out!' if you yell at him (ALL CAPS).
He says 'Fine. Be that way!' if you address him without actually saying anything.
He answers 'Whatever.' to anything else.
var Bob = function(input){.
this.hey = function(input){
if (input == input.toUpperCase() && input.match(/[a-zA-Z]/))
return 'Woah, chill out!'
else if (input.match(/\?$/g))
return 'Sure.'
else if (input.trim() == '')
return 'Fine. Be that way!'
else
return 'Whatever.'
};
};
class Bob < Struct.new(nil)
def hey(input)
which_response(input).answer
end
def answers
shouting = Shouting.new
questioning = Questioning.new
no_input = NoInput.new
whatevering = Whatevering.new
[shouting, questioning, no_input, whatevering]
end
def which_response(input)
answers.find { |answer| answer.match?(input) }
end
class Shouting
def match?(input)
input == input.upcase && input.match(/[a-zA-Z]/)
end
def answer
'Woah, chill out!'
end
end
class Questioning
def match?(input)
input.match(/\?\z/)
end
def answer
'Sure.'
end
end
class NoInput
def match?(input)
input.strip.empty?
end
def answer
'Fine. Be that way!'
end
end
class Whatevering
def match?(input)
true
end
def answer
'Whatever.'
end
end
end
var Bob = (function(){
var responses = [
{matches: function(i){i.toUpperCase() && i.match(/[a-zA-Z]/)}, response: "Woah, chill out!"},
{
];
var default_response = "Whatever.";
var bob = {
hey: function(input) {
var i, reaction found_reaction;
for(i=0; i<responses.length; i++) {
reaction = reactions[i];
if( reaction.matches(input) { found_reaction = reaction; break; }
}
return found_reaction ?
found_reaction.response :
default_reaction;
};
};
return bob;
}();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment