Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- You can add readers and writers to a class using one of the 3 commands
- ```ruby
- class Item
- attr_reader :name
- def initialize(name)
- @name = name
- end
- end
- item1 = Item.new("Clock")
- item1.name
- ```
- Will turn "Clock"
- ```ruby
- class Item
- attr_writer :name
- def initialize(name)
- @name = name
- end
- end
- item1 = Item.new("Clock")
- item1.name = "Bike"
- p item1.name
- ```
- Will return "Bike"
- attr_accessor does both attr_reader and attr_writer at the same time.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement