Last active
December 16, 2015 16:48
-
-
Save douglascamata/5465312 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.
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', 'following') | |
while command != "q" | |
printf "enter command: " | |
command = gets.chomp | |
command_processor.process(command) | |
end | |
end | |
class CommandProcessor | |
def initialize | |
@commands = Hash.new | |
@commands[:q] = "Goodbye" | |
@commands[:tweet] = "tweeting" | |
@commands[:dm] = "direct messaging" | |
@commands[:help] = "helping" | |
end | |
def add_command(command, output) | |
@commands[command.to_sym] = output | |
end | |
def process(input) | |
puts @commands[input.to_sym] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment