Created
January 4, 2019 15:11
-
-
Save Alphabetus/9b21cbb4f3af850c8ab3bec7738ce412 to your computer and use it in GitHub Desktop.
commit for exercise 3.4
This file contains 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
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