Skip to content

Instantly share code, notes, and snippets.

@danieldickison
danieldickison / attr_weak_callback.rb
Created December 13, 2013 17:42
Quick RubyMotion util to define proc callbacks that only keep a weak reference to the creator of the block/proc using the new Proc#weak! method.
module Kernel
def attr_weak_callback(*syms)
syms.each do |sym|
ivar_name = "@#{sym}".to_sym
define_method(sym) do |&block|
if block
instance_variable_set(ivar_name, block.weak!)
else
instance_variable_get(ivar_name)