Skip to content

Instantly share code, notes, and snippets.

@douglascamata
Created April 26, 2013 06:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save douglascamata/5465299 to your computer and use it in GitHub Desktop.
Save douglascamata/5465299 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) problem: use Ruby Not using objects.
def start
command = ""
commands = default_commands
add_command(commands, "follow", "following")
while command != "q"
printf "enter command: "
command = gets.chomp
process(command, commands)
end
end
def default_commands
commands = Hash.new
commands[:q] = "Goodbye"
commands[:tweet] = "tweeting"
commands[:dm] = "direct messaging"
commands[:help] = "helping"
commands
end
def add_command(commands, input, output)
commands[input.to_sym] = output
commands
end
def process(input, commands)
puts commands[input.to_sym]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment