[MacRuby-devel] #init & #initialize

Laurent Sansonetti lsansonetti at apple.com
Sun May 18 23:15:20 PDT 2008


Hi Eloy,

On May 17, 2008, at 6:55 AM, Eloy Duran wrote:

> In the RubyCocoa layer I would like to have #initialize called from
> #init,
> because in RC it was allowed to use #initialize and thus many classes
> do this.
>
> Anyways, here's some test code, please note the comments and the
> output which are basically my questions :)
>
> Cheers,
> Eloy
>
> #!/usr/local/bin/macruby
>
> class Foo
>   def init
>     if super
>       puts 'Foo init'
>       self
>     end
>   end
>
>   def initialize
>     puts 'Object'
>   end
> end
>
> class Bar < NSObject
>   def init
>     if super
>       puts 'Bar init'
>       self
>     end
>   end
>
>   def initialize
>     puts 'NSObject'
>   end
> end
>
> # Calls only initialize which is a good thing because it's a "Object"
> subclass.
> Foo.new
> # => Object

#init should be called there. It's a bug. In MacRuby, Object is a  
subclass of NSObject. All objects are therefore NSObject subclasses.

> # Calling new on a NSObject subclass calls initialize, but NOT  
> init!!!??
> Bar.new
> # => NSObject

Same than above.

> # Calls only init and NO initialize
> Bar.alloc.init
> # => Bar init

#initialize should be called there.

So, these are bugs. We need to fix them :) I guess it would be easier  
to plug the new machinery once we use the objc dispatcher for  
everything (currently we still dispatch Ruby methods separately).

Laurent


More information about the MacRuby-devel mailing list