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 # Calling new on a NSObject subclass calls initialize, but NOT init!!!?? Bar.new # => NSObject # Calls only init and NO initialize Bar.alloc.init # => Bar init # Add behaviour to have initialize called from init class NSObject # Note: Should I somehow alias the original NSObject#init? # Trying to alias it results in a NoMethodError. def init initialize self end end # Calls init and then initialize. Bar.alloc.init # => NObject # => Bar init
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
participants (2)
-
Eloy Duran
-
Laurent Sansonetti