Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.45 KB | None | 0 0
  1. You can add readers and writers to a class using one of the 3 commands
  2.  
  3. ```ruby
  4. class Item
  5. attr_reader :name
  6. def initialize(name)
  7. @name = name
  8. end
  9. end
  10.  
  11. item1 = Item.new("Clock")
  12. item1.name
  13. ```
  14. Will turn "Clock"
  15.  
  16. ```ruby
  17. class Item
  18. attr_writer :name
  19. def initialize(name)
  20. @name = name
  21. end
  22. end
  23.  
  24. item1 = Item.new("Clock")
  25. item1.name = "Bike"
  26. p item1.name
  27. ```
  28.  
  29. Will return "Bike"
  30.  
  31. attr_accessor does both attr_reader and attr_writer at the same time.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement