Advertisement
Guest User

Untitled

a guest
Jul 25th, 2016
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. class Pet
  2. attr_reader :color, :breed
  3. attr_accessor :name, :speak
  4. def initialize(color, breed)
  5. @color = color
  6. @breed = breed
  7. @hungry = true
  8. end
  9.  
  10. def feed(food)
  11. puts "Mmm, " + food + "!"
  12. @hungry = false
  13. end
  14.  
  15. def hungry?
  16. if @hungry
  17. puts "I'm hungry!"
  18. else
  19. puts "I'm full!"
  20. end
  21. @hungry
  22. end
  23. end
  24.  
  25. class Cat < Pet
  26. def speak
  27. puts "meow!"
  28. end
  29. end
  30.  
  31. class Dog < Pet
  32. def speak
  33. puts "Woof!"
  34. end
  35. end
  36.  
  37. doggy = Dog.new("black", "pitbull")
  38.  
  39. doggy.name = "scruffy"
  40. puts "Your new dog is called #{doggy.name}"
  41. puts "The color of this dog is #{doggy.color} and it's breed is #{doggy.breed}"
  42.  
  43. puts "Are you hungry, Scruffy?"
  44. doggy.hungry?
  45. doggy.feed("cake")
  46. puts "and now.. ? Still hungry after all that?"
  47. doggy.hungry?
  48.  
  49. puts "Ahh you're a cute dog, #{doggy.name}, do you do anything besides eat?"
  50. 3.times do
  51. doggy.speak
  52. end
  53.  
  54. kitty = Cat.new("grey", "Persian")
  55.  
  56. puts "Let's inspect our new cat"
  57. puts kitty.inspect
  58. puts "What class does our new cat belong to?"
  59. puts kitty.class
  60. puts "Is our new cat an object?"
  61. puts kitty.is_a?(Object)
  62.  
  63. puts "What color is my cat?"
  64. puts kitty.color
  65.  
  66. puts "and... the breed of the cat?"
  67. puts kitty.breed
  68.  
  69. puts "What's the name of the cat?"
  70. kitty.name = gets.scrub
  71. puts "So just to confirm, your new cat will be called #{kitty.name}"
  72.  
  73. puts "Is our cat hungry now?"
  74. kitty.hungry?
  75. puts "Let's feed our cat"
  76. kitty.feed("tuna")
  77. puts "Is our cat hungry now?"
  78. kitty.hungry?
  79.  
  80. puts "*tickle cat*"
  81. kitty.speak
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement