Skip to content

Instantly share code, notes, and snippets.

@Alphabetus
Created Jan 4, 2019
Embed
What would you like to do?
commit for exercise 3.5
class Pet
#attr handler
attr_reader :color, :breed
attr_accessor :name
#init method
def initialize(color, breed)
@color = color
@breed = breed
@hungry = true
end
# class methods
def feed(food)
puts "Nhammmm, " + food + "!"
@hungry = false
end
def hungry?
if @hungry
puts "Hey it's hungry!"
else
puts "Nah... it's good!"
end
end
end
class Cat < Pet
def speak
puts "Meow"
end
end
class Dog < Pet
def speak
puts "Wooof"
end
end
# RUN
puts "Hello there... Let's create an animal Pet for you."
puts "What animal do you wan? A cat or a dog?"
animal = gets.chomp # pick input
case animal.downcase
when "cat"
pet = Cat.new("grey", "Persian")
when "dog"
pet = Dog.new("brown", "Pit Bull")
else
puts "ups... i cant recognize that answer.. please restart program."
return
end
puts "We created your "+ animal.downcase + "."
puts "Let's inspect our new #{pet.class}:"
puts pet.inspect
puts "What class does our new #{pet.class} belong to?"
puts pet.class
puts "Is our new #{pet.class} an object?"
puts pet.is_a?(Object)
puts "What color is our #{pet.class}?"
puts pet.color
puts "What breed is our #{pet.class}?"
puts pet.breed
puts "---" * 20 # line separator
puts "Let's name our #{pet.class}" #i wanna name my own cat :)
pet.name = gets.chomp
puts "You named it #{pet.name}."
puts "---" * 20 # line separator
puts "Is our #{pet.class} hungry now?"
pet.hungry?
puts "Let's feed our #{pet.class}, pick your food"
food = gets.chomp # i wanna pick my own food
pet.feed(food)
puts "Is our #{pet.class} hungry now?"
pet.hungry?
puts "---" * 20 # line separator
puts "Our #{pet.class} can talk, would you like to hear it? yes, no or maybe" # im not sure if i wanna hear the Meow, might be too late.
answer = gets.chomp #get answer
case answer
when "yes"
puts "Here it goes... get ready!"
pet.speak
when "no"
puts "Ok, you told it to be quiet."
when "maybe"
answerArray = ["yes", "no"] # lot of answers
answerArray.shuffle! #randomize lot of answers
# define final action based on answer
if answerArray[0] == "yes"
puts "Ok.. the #{pet.class} decided to talk..."
pet.speak
else
puts "This time the #{pet.class} remained silent..."
end
else # wrong answer dude.
puts "My small brain only accept 3 answers... my apologies."
end
puts "---" * 20
puts "Program ended\n\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment