In rubymotion (and I think MacRuby shares this behavior) there are TWO types of classes: pure Ruby, which use initialize
, and classes derived from an objective-c class. It is best not to mix the two. If you are subclassing a objective-c class, you should use an init
method. your initialize
method will never get called!
This is because the NSObject##new
method calls init
, not initialize
(actually even this is an oversimplification, but the abstraction works).
your example above is confusing because you are calling init
instead of initWithFrame
. You MUST call the designated initializer of the parent class - in this case, using super
.
If the initializer you call (in this case, init
, calls your initializer, you're using the wrong one - there's no escaping the recursion cycle in that case.
So you can't exactly CHOOSE which method to call on the parent. You can, however, use your own init method:
def initWithTitle(title)
initWithFrame(CGRectZero)
@title = title
return self
end
I would write this method like this:
def initWithTitle(title)
initWithFrame(CGRectZero).tap do
@title = title
end # that way I never forget to return self
end