Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Create a parent class for Pets
- class Pets
- attr_reader :color, :breed
- attr_accessor :name
- def initialize(color, breed)
- @color = color
- @breed = breed
- @hungry = true
- end
- def feed(food)
- puts "Mmmmm, " + food + "!"
- @hungry = false
- end
- def hungry?
- if @hungry
- puts "I'm hungry!"
- else
- puts "I'm full!"
- end
- @hungry
- end
- end
- # Create a child class for Cat
- class Cat < Pets
- def speak
- puts "Meow-choooo!!"
- end
- end
- # Create a child class for Dog
- class Dog < Pets
- def speak
- puts "Woof! Woof!"
- end
- end
- # Initiate the kitty object
- 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 "Now let's give our new cat a name:"
- kitty.name = "Sneezy"
- puts kitty.name
- puts "Is our cat hungry now?"
- kitty.hungry?
- puts "Let's feed our cat."
- kitty.feed("tuna")
- puts "Is our cat hungry now?"
- kitty.hungry?
- puts "Our cat can make noise:"
- kitty.speak
- # Initiate the puppy object
- puppy = Dog.new("White and black", "Spaniel")
- puts "Let's inspect our new puppy:"
- puts puppy.inspect
- puts "What does our puppy say?"
- puppy.speak
- puts "Is our puppy hungry?"
- puppy.hungry?
- puts "Let's feed our puppy"
- puppy.feed("pot roast")
- puts "Is our puppy hungry now?"
- puppy.hungry?
- puts "What color is our puppy?"
- puts puppy.color
- puts "Is our new puppy an object?"
- puts puppy.is_a?(Object)
- puts "What class does our puppy belong to?"
- puts puppy.class
- puts "Well, what should we name our new puppy?"
- puppy.name = "Frodo"
- puts puppy.name
Add Comment
Please, Sign In to add comment