[MacRuby-devel] Obj-C object allocation and consistency in Ruby

Brian Mitchell binary42 at gmail.com
Thu May 29 06:07:08 PDT 2008

I've been playing around with objects a bit and have noticed the
Objective-C interfaces for object allocation are available. I'm not an
regular Objective-C user so this is all from my limited knowledge.

In MacRuby, one can currently get away with something like
NSCFString.alloc, and it seems to not cause any immediate crashes if
init is not called right after. This is a dangerous assumption of
course. If I simply try calling on a different class where init is
more important it will quickly cause a crash:

In Objective-C the pattern is (AFAICT) usually something like
[[MyClass alloc] initOfSomeSort]. It makes sense and the
responsibility is on the developer to keep things consistent. In Ruby
code however, I would expect these sorts of issues to either be
handled or be impossible to occur in pure Ruby. Now there might be a
way to handle errors better (i.e. catching the
NSInvalidArgumentException in the above example) and prevent bad
crashes that currently occur but is this sort of thing just going to
become a side effect of living on top of an Objective-C style class
hierarchy and protocol? I'd really like to avoid getting segmentation
faults and other lower level problems in Ruby code. (Though I don't
mind paying the price if this gets to what 1.0 has promised. ;-) )


More information about the MacRuby-devel mailing list