[MacRuby-devel] Elegant initialization

Joshua Ballanco jballanc at gmail.com
Wed Apr 18 21:29:55 PDT 2012


Welcome! Hope you enjoy your stay!

You can write Obj-C-ish methods in MacRuby the same way you call them: using Ruby's hash syntax. The only other caveat is the difference between "super" in Ruby and Obj-C. In Obj-C, "super" is the superclass. In Ruby, it is shorthand for calling the same method in the superclass. Also, in Ruby, you are not allowed to redefine self. Finally, you generally don't need to worry about weak references in Ruby (since cycles can be handled by the GC). So your above method translated would look like this:

def initWithFrame(frame, controller: ctrlr)
  initWithFrame(frame)
  @controller = ctrlr
  self
end

Cheers,

Josh 


On Wednesday, April 18, 2012 at 11:44 PM, Josh Voigts wrote:

> Hey, first post to the list, woot!
> 
> I've run into a little bit of a snag. Is there an elegant way of
> building an initializer similar to this one in macruby? (assuming I'm
> subclassing an NSView object)
> 
> 
> - (id)initWithFrame:(NSRect)frame controller:(AppController *)ctrlr
> {
>     if (self = [super initWithFrame:frame]) {
>         controller = ctrlr; // deliberately weak reference.
>     }
> 
>     return self;
> }
> _______________________________________________
> MacRuby-devel mailing list
> MacRuby-devel at lists.macosforge.org (mailto:MacRuby-devel at lists.macosforge.org)
> http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
> 
> 


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-devel/attachments/20120419/141f66e2/attachment.html>


More information about the MacRuby-devel mailing list