Skip to content

Instantly share code, notes, and snippets.

@douglascamata
Last active December 16, 2015 16:48
Show Gist options
  • Save douglascamata/5465378 to your computer and use it in GitHub Desktop.
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.
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