Skip to content

Instantly share code, notes, and snippets.

@kvirani
Last active September 12, 2017 09:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 86 You must be signed in to fork a gist
  • Save kvirani/871340ef674ec56a6582 to your computer and use it in GitHub Desktop.
Save kvirani/871340ef674ec56a6582 to your computer and use it in GitHub Desktop.
Shakil The Dog - Ruby Exercise

Your cofounder has left for Hawaii and asked that you take care of his puppy for 2 weeks.

As it turns out, this dog is a menace and won’t stop barking. His name is Shakil. He exhibits the following inexplicable behavior:

If you say anything to him, he will just bark back once ("woof"), except:

  • If you pretend to be a dog and bark ("woof") at him, he will go a bit nuts and woof back at you three times, pretty loudly: "WOOF WOOF WOOF"
  • If you explicitly use his name and tell him to stop (either "shakil stop" or "Shakil STOP!") he will not respond back with a bark (enjoy your moment of peace)
  • If you pretend to be a cat and "meow" at him, he will go berserk and woof back at you five times: "woof woof woof woof woof"
  • If you say anything else but with the word "treat" thrown into the mix, he won’t bark back, thinking he’ll be getting a treat
  • If you say "go away" he manages to actually leave you alone by leaving the room.

We'll use a method in our ruby file to simulate the interaction with this creature.

How should these behaviors map to code?

  • Starting a conversation with the silly dog implies running the program via the command line
  • Having an endless conversation with Shakil implies a loop in the program
  • Speaking to Shakil implies reading a line of user input with the gets method
  • Shakil responding to you (usually by barking) implies the program printing a line using the puts method
  • Leaving the room implies exiting the program

As you are coding this, ask yourself:

  1. Are you writing a single, gigantic method or breaking down your program into logical units?
  2. Are you trying to write the entire logic for your app in one go? Instead consider writing the program in incremental steps by starting with the simplest runnable code, running it, adding slightly more code/logic, running it, and so on?
# Save this file to your computer so you can run it
# via the command line (Terminal) like so:
# $ ruby shakil_the_dog.rb
#
# Your method should wait for user input, which corresponds
# to you saying something to your dog (named Shakil).
# You'll probably want to write other methods, but this
# encapsulates the core dog logic
def shakil_the_dog
end
# Run our method
shakil_the_dog
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment