'accessors'에 해당되는 글 1건
Ruby, accessorsThe Day of Code
attr_reader, attr_accessor 는 뭘까요?
흥미만 잔뜩 있던, Ruby에 대해서 알고 싶기도 하고, 리팩터링 책도 읽지 못해본 상태라, 두마리 토끼를 잡아보려, "리팩토링:루비 에디션" 을 읽고 있습니다.
그런데 첫 코드에서 막혀 버렸습니다.
나머지는 대충 알겠는데, attr_reader, attr_accessor 요녀석들은 뭘까요?
class Movie
REGULAR = 0
NEW_RELEASE =1
CHILDRENS =2
attr_reader :title
attr_accessor :price_code
def initialize(title, price_code)
@title, @price_code = title, price_code
end
end
Accessors
Yukihiro Matsumoto 님의 Ruby User’s Guide에 쉽게 설명이 있네요.
| Shortcut | Effect |
|---|---|
| attr_reader :v | def v; @v; end |
| attr_writer :v | def v=(value); @v=value; end |
| attr_accessor :v | attr_reader :v; attr_writer :v |
| attr_accessor :v, :w | attr_accessor :v; attr_accessor :w |