Skip to content

Instantly share code, notes, and snippets.

@tim-br
Created June 30, 2015 22:28
Show Gist options
  • Save tim-br/858a325a79c138df84e0 to your computer and use it in GitHub Desktop.
Save tim-br/858a325a79c138df84e0 to your computer and use it in GitHub Desktop.
# 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
@kvirani
Copy link

kvirani commented Jul 5, 2015

Much better @tmtwd, a few notes:

  1. Change it to a .rb file so it syntax highlights
  2. Avoid using exit and instead break out of the infinite while loop when input is go away
  3. Missing white space issue here: 5.times {puts "woof"}
  4. eval method content is indented 2x for some reason (compared to the other method, for example) - plz fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment