WeakRef

WeakRef

참조하는 객체가 가비지 컬렉션될 수 있도록 허용하는 약한 참조(Weak Reference) 클래스예요. WeakRef는 참조하는 객체처럼 그대로 사용할 수 있어요.

foo = Object.new            # create a new object instance
p foo.to_s                  # original's class
foo = WeakRef.new(foo)      # reassign foo with WeakRef instance
p foo.to_s                  # should be same class
GC.start                    # start the garbage collector
p foo.to_s                  # should raise exception (recycled)

출처: Ruby 3.3 API

본문

new(orig) → weakref

orig에 대한 약한 참조를 만들어요.

weakref_alive? → true or false

참조된 객체가 아직 살아 있으면 true를 돌려줘요.