Created
June 30, 2015 22:28
-
-
Save tim-br/858a325a79c138df84e0 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# 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 | |
#regex = Regexp.new(Regexp.escape("treat"), Regexp::IGNORECASE) | |
while true | |
input = gets.chomp.downcase | |
if input == "go away" | |
puts "The dog has left the room" | |
exit | |
end | |
eval(input) | |
end | |
end | |
def eval(input) | |
if input == "woof" | |
4.times { puts "woof" } | |
elsif input == "shakil stop" | |
puts "quiet dog " | |
elsif input == "meow" | |
5.times {puts "woof"} | |
elsif (/treat/i).match input | |
puts "regex input" | |
else | |
puts "woof" | |
end | |
end | |
shakil_the_dog |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Much better @tmtwd, a few notes:
exit
and instead break out of the infinitewhile
loop when input is go away5.times {puts "woof"}
eval
method content is indented 2x for some reason (compared to the other method, for example) - plz fix.