Last active
December 16, 2015 16:48
-
-
Save douglascamata/5465378 to your computer and use it in GitHub Desktop.
Another solution to this post (http://jumpstartlab.com/news/archives/2013/04/23/the-death-of-ifs) with objects that can do real stuff, like Twitter (or any other social network, i.e.) API calls.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def start | |
command = "" | |
command_processor = CommandProcessor.new | |
command_processor.add_command('follow', Proc.new { puts 'do some stuff' }) | |
while command != "q" | |
printf "enter command: " | |
command = gets.chomp | |
command_processor.process(command) | |
end | |
end | |
class CommandProcessor | |
def initialize | |
@commands = Hash.new | |
@commands[:q] = Proc.new { "Goodbye" } | |
@commands[:tweet] = Proc.new { "tweeting" } | |
@commands[:dm] = Proc.new { "direct messaging" } | |
@commands[:help] = Proc.new { "helping" } | |
end | |
def add_command(command, output) | |
@commands[command.to_sym] = output | |
end | |
def process(input) | |
(@commands[input.to_sym] || Proc.new {} ).call | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment