Advertisement
Guest User

Untitled

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