Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $dogkilled = false
- $catalive = true
- class Pet
- attr_reader :color, :breed
- attr_accessor :name
- def initialize(color, breed)
- @color = color
- @breed = breed
- @hungry = true
- @fight = false
- end
- def feed(food)
- p "Mmmm, " + food + "!"
- @hungry = false
- end
- def hungry?
- if @hungry
- p "I\'m hungry!"
- else
- p "I\'m full!"
- end
- @hungry
- end
- def meet
- p "WuffWuff Knrrrrrrrr Wufff Meeeeeooooowww"
- $dogkilled = true
- $catalive = false
- end
- end
- class Cat < Pet
- def speak
- p "Meow!"
- end
- def alive?
- if $catalive
- p "Of course, I am alive" #there shouldn't be a fight - cat is alive
- else
- p "Cat: ..." #there was a fight cat is dead
- end
- end
- end
- class Dog < Pet
- def speak
- p "WuffWuff!"
- end
- def alive?
- if $dogkilled
- p "Man, that was worth it!" #Won the fight
- else
- p "Hey...I tried to sleep" #no fight - sleeping dog
- end
- end
- end
- kitty = Cat.new("grey", "Persian")
- p "Let's inspect our new cat:"
- p kitty.inspect
- p "What class does our new cat belong to?"
- p kitty.class
- p "Is our new cat an object?"
- p kitty.is_a?(Object)
- p "What color is our cat?"
- p kitty.color
- p "Let\'s give our new cat a name"
- kitty.name ="Betsy"
- p kitty.name
- p "Is our cat hungry now?"
- kitty.hungry?
- p "Let\'s feed our cat"
- kitty.feed("tuna")
- p "Is our cat hungry now?"
- kitty.hungry?
- puts "Our cat can make some noise"
- kitty.speak
- puppy = Dog.new("black", "Staffordshire Terrier")
- p "Hey our dog can make some noise too"
- puppy.speak
- p "Breed:"
- puts puppy.breed
- puts "THE FIGHT"
- puppy.meet
- kitty.alive?
- puppy.alive?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement