Skip to content

Instantly share code, notes, and snippets.

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