Advertisement
Guest User

Untitled

a guest
Apr 27th, 2015
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 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 # instance variable
  7. @breed = breed # instance variable
  8. @hungry = true
  9. end
  10.  
  11. def feed(food)
  12. puts "Mmm, " + food + "!"
  13. @hungry = false
  14. end
  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. # Instance variables belong to each instance of a given class.
  28.  
  29. kitty = Cat.new("grey", "Persian")
  30.  
  31. puts "Let\'s inspect our new cat:"
  32. puts kitty.inspect
  33.  
  34. puts "What class does our new cat belong to?"
  35. puts kitty.class
  36.  
  37. puts "is our new cat an object"
  38. puts kitty.is_a?(Object)
  39.  
  40. puts "What color is our cat?"
  41. puts kitty.color
  42.  
  43. puts "Let\'s give our cat a name."
  44. kitty.name = "Lazengann Overload!"
  45. puts kitty.name
  46.  
  47. puts "Is our cat hungry now?"
  48. kitty.hungry?
  49.  
  50. puts "Let\'s feed our cat"
  51. kitty.feed("tuna")
  52.  
  53. puts "Is our cat hungry now?"
  54. kitty.hungry?
  55.  
  56. puts "Our cat can make noise"
  57. kitty.speak
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement