Advertisement
Guest User

Untitled

a guest
Jul 28th, 2015
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. $dogkilled = false
  2. $catalive = true
  3.  
  4. class Pet
  5. attr_reader :color, :breed
  6. attr_accessor :name
  7. def initialize(color, breed)
  8. @color = color
  9. @breed = breed
  10. @hungry = true
  11. @fight = false
  12. end
  13. def feed(food)
  14. p "Mmmm, " + food + "!"
  15. @hungry = false
  16. end
  17. def hungry?
  18. if @hungry
  19. p "I\'m hungry!"
  20. else
  21. p "I\'m full!"
  22. end
  23. @hungry
  24. end
  25. def meet
  26. p "WuffWuff Knrrrrrrrr Wufff Meeeeeooooowww"
  27. $dogkilled = true
  28. $catalive = false
  29. end
  30. end
  31.  
  32. class Cat < Pet
  33.  
  34. def speak
  35. p "Meow!"
  36. end
  37. def alive?
  38. if $catalive
  39. p "Of course, I am alive" #there shouldn't be a fight - cat is alive
  40. else
  41. p "Cat: ..." #there was a fight cat is dead
  42. end
  43. end
  44. end
  45.  
  46. class Dog < Pet
  47. def speak
  48. p "WuffWuff!"
  49. end
  50. def alive?
  51. if $dogkilled
  52. p "Man, that was worth it!" #Won the fight
  53. else
  54. p "Hey...I tried to sleep" #no fight - sleeping dog
  55. end
  56. end
  57. end
  58.  
  59.  
  60.  
  61. kitty = Cat.new("grey", "Persian")
  62.  
  63. p "Let's inspect our new cat:"
  64. p kitty.inspect
  65. p "What class does our new cat belong to?"
  66. p kitty.class
  67. p "Is our new cat an object?"
  68. p kitty.is_a?(Object)
  69. p "What color is our cat?"
  70. p kitty.color
  71. p "Let\'s give our new cat a name"
  72. kitty.name ="Betsy"
  73. p kitty.name
  74. p "Is our cat hungry now?"
  75. kitty.hungry?
  76. p "Let\'s feed our cat"
  77. kitty.feed("tuna")
  78. p "Is our cat hungry now?"
  79. kitty.hungry?
  80. puts "Our cat can make some noise"
  81. kitty.speak
  82.  
  83.  
  84. puppy = Dog.new("black", "Staffordshire Terrier")
  85. p "Hey our dog can make some noise too"
  86. puppy.speak
  87. p "Breed:"
  88. puts puppy.breed
  89.  
  90.  
  91. puts "THE FIGHT"
  92. puppy.meet
  93. kitty.alive?
  94. puppy.alive?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement