{
"$type": "site.standard.document",
"description": "This is probably obvious to Ruby developers, but for us Objective C developers who are new to RubyMotion, I've found it common enough to define",
"path": "/bool-attribute-accessors-in-rubymotion/",
"publishedAt": "2014-02-28T12:34:00.000Z",
"site": "at://did:plc:bryys25pc2fnagnyxqgsglhd/site.standard.publication/3mn26bjkkmh23",
"tags": [
"RubyMotion"
],
"textContent": "This is probably obvious to Ruby developers, but for us Objective C developers who are new to RubyMotion, I've found it common enough to define accessors for bools.\n\nBool accessors end with a question mark (?), so normally you do this:\n\nclass SomeClass\n attr_accessor :friendly\n\n def friendly?\n friendly\n end\nend\n\nor\n\nclass SomeClass\n attr_accessor :friendly\n alias_method :friendly?, :friendly\nend\n\nbut with a little DSL:\n\nclass Class\n def bool_attr_accessor(*my_accessors)\n my_accessors.each do |accessor|\n define_method((\"#{accessor}?\").to_sym) do\n !!instance_variable_get(\"@#{accessor}\")\n end\n \n define_method(\"#{accessor}=\") do |accessor_value|\n instance_variable_set(\"@#{accessor}\", accessor_value)\n end\n end\n end\nend\n\nYou can do this instead:\n\nclass SomeClass\n bool_attr_accessor :friendly\nend",
"title": "Bool Attribute Accessors in RubyMotion"
}