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.4
class Cat
#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
def speak
puts "Meow"
end
#
end
kitty = Cat.new("grey", "Persian")
puts "Let's inspect our new cat:"
puts kitty.inspect
puts "What class does our new cat belong to?"
puts kitty.class
puts "Is our new cat an object?"
puts kitty.is_a?(Object)
puts "What color is our cat?"
puts kitty.color
puts "What breed is our cat?"
puts kitty.breed
puts "---" * 20 # line separator
puts "Let's name our cat" #i wanna name my own cat :)
kitty.name = gets.chomp
puts "You named it #{kitty.name}."
puts "---" * 20 # line separator
puts "Is our cat hungry now?"
kitty.hungry?
puts "Let's feed our cat, pick your food"
food = gets.chomp # i wanna pick my own food
kitty.feed(food)
puts "Is our cat hungry now?"
kitty.hungry?
puts "---" * 20 # line separator
puts "Our cat 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!"
kitty.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 cat decided to talk..."
kitty.speak
else
puts "This time the cat 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