Guest User

Untitled

a guest
Jan 17th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. class Cat
  2. attr_reader :color, :breed
  3. attr_accessor :name
  4.  
  5. def initialize(color, breed)
  6. @color = color
  7. @breed = breed
  8. @hungry = 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 speak
  26. puts "Meow!"
  27. end
  28.  
  29.  
  30.  
  31. end
  32.  
  33. kitty = Cat.new("grey", "Persian")
  34.  
  35. puts "Let's inspect our new cat:"
  36. puts kitty.inspect
  37. puts "What class does our new cat belong to?"
  38. puts kitty.class
  39. puts "Is our new cat an object?"
  40. puts kitty.is_a?(Object)
  41.  
  42.  
  43. puts "What color is our cat?"
  44. puts kitty.color
  45.  
  46.  
  47. puts "Let's give our new cat a name"
  48. kitty.name = "Betsy"
  49. puts kitty.name
  50.  
  51. puts "Is our cat hungry now?"
  52. kitty.hungry?
  53. puts "Let's feed our cat"
  54. kitty.feed("tuna")
  55. puts "Is our cat hungry now?"
  56. kitty.hungry?
  57.  
  58. puts "Our cat can make noise"
  59. kitty.speak
Add Comment
Please, Sign In to add comment