Guest User

Untitled

a guest
Jan 17th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. # Create a parent class for Pets
  2. class Pets
  3. attr_reader :color, :breed
  4. attr_accessor :name
  5.  
  6. def initialize(color, breed)
  7. @color = color
  8. @breed = breed
  9. @hungry = true
  10. end
  11.  
  12. def feed(food)
  13. puts "Mmmmm, " + food + "!"
  14. @hungry = false
  15. end
  16.  
  17. def hungry?
  18. if @hungry
  19. puts "I'm hungry!"
  20. else
  21. puts "I'm full!"
  22. end
  23. @hungry
  24. end
  25. end
  26.  
  27. # Create a child class for Cat
  28. class Cat < Pets
  29. def speak
  30. puts "Meow-choooo!!"
  31. end
  32. end
  33.  
  34. # Create a child class for Dog
  35. class Dog < Pets
  36. def speak
  37. puts "Woof! Woof!"
  38. end
  39. end
  40.  
  41. # Initiate the kitty object
  42. kitty = Cat.new("grey", "Persian")
  43.  
  44. puts "Let's inspect our new cat:"
  45. puts kitty.inspect
  46. puts "What class does our new cat belong to?"
  47. puts kitty.class
  48. puts "Is our new cat an object?"
  49. puts kitty.is_a?(Object)
  50.  
  51. puts "What color is our cat?"
  52. puts kitty.color
  53. puts "What breed is our cat?"
  54. puts kitty.breed
  55.  
  56. puts "Now let's give our new cat a name:"
  57. kitty.name = "Sneezy"
  58. puts kitty.name
  59.  
  60. puts "Is our cat hungry now?"
  61. kitty.hungry?
  62. puts "Let's feed our cat."
  63. kitty.feed("tuna")
  64. puts "Is our cat hungry now?"
  65. kitty.hungry?
  66. puts "Our cat can make noise:"
  67. kitty.speak
  68.  
  69. # Initiate the puppy object
  70. puppy = Dog.new("White and black", "Spaniel")
  71.  
  72. puts "Let's inspect our new puppy:"
  73. puts puppy.inspect
  74. puts "What does our puppy say?"
  75. puppy.speak
  76. puts "Is our puppy hungry?"
  77. puppy.hungry?
  78. puts "Let's feed our puppy"
  79. puppy.feed("pot roast")
  80. puts "Is our puppy hungry now?"
  81. puppy.hungry?
  82. puts "What color is our puppy?"
  83. puts puppy.color
  84. puts "Is our new puppy an object?"
  85. puts puppy.is_a?(Object)
  86. puts "What class does our puppy belong to?"
  87. puts puppy.class
  88. puts "Well, what should we name our new puppy?"
  89. puppy.name = "Frodo"
  90. puts puppy.name
Add Comment
Please, Sign In to add comment