class Belhyun
class << self
def belhyun2
p 'belhyun2'
end
end
end
Belhyun.belhyun2
위 결과 값은 'belhyun2'입니다.
class << self는 Belhyun에 대한 싱글턴 객체를 열게(open up) 됩니다. 그리고 위 구문은 이 싱글턴 객체에 메소드를 재정의하게 됩니다. 즉 Belhyun에 static 메소드를 정의한 것과 같은 효과를 내게 됩니다. 주로 사용되는 곳도 static 메소드를 정의할 때 사용됩니다.
댓글 없음:
댓글 쓰기