Skip to content

Instantly share code, notes, and snippets.

@business-phil
Last active November 1, 2018 13:10
Show Gist options
  • Select an option

  • Save business-phil/648627320922edae10638a4eec11271c to your computer and use it in GitHub Desktop.

Select an option

Save business-phil/648627320922edae10638a4eec11271c to your computer and use it in GitHub Desktop.
DOGBOT
class Dogbot
attr_accessor :dogpile
def initialize(&block)
@dogpile = []
instance_eval(&block)
end
def pet_doggos
@dogpile.each { |dog| puts "#{dog.name} says \"#{dog.bark}\"" }
end
def play_with_doggos
@dogpile.each { |dog| puts "#{dog.name} can #{dog.play}" }
end
def good_dog(name, &block)
doggo = Doggo.new(name: name, bark: 'bow wow', &block)
@dogpile << doggo
end
def bad_dog(name, &block)
doggo = Doggo.new(name: name, bark: 'grrr', &block)
@dogpile << doggo
end
end
class Doggo
attr_accessor :name, :bark, :tricks
def initialize(name:, bark: '', &block)
@name = name
@bark = bark
@tricks = []
instance_eval(&block) if block_given?
end
def teach(trick, slang = '')
@tricks << trick
if trick == 'talk like human' && !slang.empty?
@bark = slang
end
end
def play
@tricks.empty? ? 'do nothing' : @tricks.sample
end
end
# Test Dogbot
dogbot = Dogbot.new do
bad_dog 'Gracie' do
teach 'fetch'
teach 'shake'
teach 'bake'
teach 'talk like human'
end
good_dog 'Spencer'
bad_dog 'Muffin' do
teach 'sit'
teach 'talk like human', 'Howdy'
end
end
dogbot.pet_doggos
dogbot.play_with_doggos
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment