Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. class Pet
  2. attr_reader :color, :breed, :name
  3. attr_accessor :name
  4. def initialize(color,breed)
  5. @color = color
  6. @breed = breed
  7. @hungry = true
  8. @thirsty = true
  9. end
  10.  
  11. def feed(food)
  12. puts "Mmmm, " + food + "!"
  13. @hungry = false
  14. end
  15.  
  16. def hungry?
  17. if @hungry
  18. puts "I'm hungry!"
  19. else
  20. puts "I'm full!"
  21. end
  22. @hungry
  23. end
  24.  
  25. def thirst(liquid)
  26. puts "Mmmm, " + liquid + "!"
  27. @thirsty = false
  28. end
  29.  
  30. def thirsty?
  31. if @thirsty
  32. puts "I'm thirsty now!"
  33. else
  34. puts "I'm satisfied now!"
  35. end
  36. @thirsty
  37. end
  38. end
  39.  
  40. class Cat < Pet #inherits from Pet class
  41. def speak
  42. puts "Meow!"
  43. end
  44. end
  45.  
  46. class Dog < Pet
  47. def speak
  48. puts "Woof!"
  49. end
  50. end
  51.  
  52. kitty = Cat.new("Grey","Persian")
  53. dog = Dog.new("Brown","German Sheperd")
  54.  
  55. puts "Let's inspect our new cat:"
  56. puts kitty.inspect
  57. puts "What class does our new cat belong to?"
  58. puts kitty.class
  59. puts "Is our new cat an object?"
  60. puts kitty.is_a?(Object)
  61. puts "What color is our cat?"
  62. puts kitty.color
  63. puts "Let's give our new cat a name"
  64. kitty.name = "Jack"
  65. puts kitty.name
  66. puts "Is our cat hungry now?"
  67. kitty.hungry?
  68. puts "Let's feed our cat"
  69. kitty.feed("meat")
  70. puts "Is our cat hungry now?"
  71. kitty.hungry?
  72. puts "Our cat can make noise"
  73. kitty.speak
  74. puts "Is our cat thirsty?"
  75. kitty.thirsty?
  76. puts "Let's give something to drink"
  77. kitty.thirst("water")
  78. puts "Is our cat thirsty?"
  79. kitty.thirsty?
  80.  
  81. puts "Let's inspect our new dog:"
  82. puts dog.inspect
  83. puts "What class does our new dog belong to?"
  84. puts dog.class
  85. puts "Is our new dog an object?"
  86. puts dog.is_a?(Object)
  87. puts "What color is our dog?"
  88. puts dog.color
  89. puts "Let's give our new dog a name"
  90. dog.name = "Jack"
  91. puts dog.name
  92. puts "Is our dog hungry now?"
  93. dog.hungry?
  94. puts "Let's feed our dog"
  95. dog.feed("meat")
  96. puts "Is our dog hungry now?"
  97. dog.hungry?
  98. puts "Our dog can make noise"
  99. dog.speak
  100. puts "Is our dog thirsty?"
  101. dog.thirsty?
  102. puts "Let's give something to drink"
  103. dog.thirst("water")
  104. puts "Is our dog thirsty?"
  105. dog.thirsty?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement