{
  "$type": "site.standard.document",
  "path": "/weak-attr-accessors-in-rubymotion/",
  "publishedAt": "2014-02-01T17:12:00.000Z",
  "site": "at://did:plc:bryys25pc2fnagnyxqgsglhd/site.standard.publication/3mn26bjkkmh23",
  "tags": [
    "RubyMotion"
  ],
  "textContent": "It's useful – and crucial – in RubyMotion to define attribute accessors so only a weak reference is held to avoid cyclic redundancies.\n\nYou can define a simple DSL:\n\nclass Class\n  def weak_attr_accessor(*my_accessors)\n    my_accessors.each do |accessor|\n      define_method(accessor) do\n        instance_variable_get(\"@#{accessor}\")\n      end\n \n      define_method(\"#{accessor}=\") do |accessor_value|\n        instance_variable_set(\"@#{accessor}\", WeakRef.new(accessor_value))\n      end\n    end\n  end\nend\n\nThen use it like this:\n\nclass SomeView < UIView\n  weak_attr_accessor :view_controller\nend\n\nI'm pretty sure I copied (and maybe modified the code a little) from this gist, but it looks like the link doesn't work anymore.\n\nUPDATE: Exactly 1 year later (no kidding), I finally wrote a weak_attr_accessor because I wanted to use it in purplish-layout. See also bool attribute accessors in RubyMotion.",
  "title": "Weak Attribute Accessors in RubyMotion"
}