Objective-C class +initialize in MacRuby
Hi, It looks like self.initialize is a no-go: class Foo def self.initialize NSLog("self.initialize is a saaaad panda") # <- never runs end end The ruby idiom for that would be just: class Foo NSLog("IM IN UR CLASS INITIALIZEEN FROM RUBY") end is that ok in the context of Cocoa applications? Is there a better way?
$ macruby -e "class Foo; def initialize; puts '10/10/10'; end; end; Foo.new" 10/10/10 The problem in your example is that in Ruby, the constructor is an instance method, not a class method. So your mistake was to define self.initialize instead of just initialize. class Foo def initialize puts "initialize is a haaaapy panda" end end Foo.new - Matt On Sun, Oct 10, 2010 at 11:35 AM, Caio Chassot <lists@caiochassot.com>wrote:
Hi,
It looks like self.initialize is a no-go:
class Foo def self.initialize NSLog("self.initialize is a saaaad panda") # <- never runs end end
The ruby idiom for that would be just:
class Foo NSLog("IM IN UR CLASS INITIALIZEEN FROM RUBY") end
is that ok in the context of Cocoa applications? Is there a better way?
_______________________________________________ MacRuby-devel mailing list MacRuby-devel@lists.macosforge.org http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
On 2010-10-10, at 15:15 , Matt Aimonetti wrote:
The problem in your example is that in Ruby, the constructor is an instance method, not a class method. So your mistake was to define self.initialize instead of just initialize.
I'm afraid you misunderstood my question. I'm not talking about -init / #initialize. I'm talking about the crazy special +initialize method that Objective-C calls when it first loads a class. You can read about all its idiosyncrasies here: http://mikeash.com/pyblog/friday-qa-2009-05-22-objective-c-class-loading-and... Its lazy nature is what irks me. Code right in the class body in ruby seems to be more in line with Objective-C's +load.
Hi Caio, On Oct 10, 2010, at 9:35 AM, Caio Chassot wrote:
Hi,
It looks like self.initialize is a no-go:
class Foo def self.initialize NSLog("self.initialize is a saaaad panda") # <- never runs end end
The ruby idiom for that would be just:
class Foo NSLog("IM IN UR CLASS INITIALIZEEN FROM RUBY") end
is that ok in the context of Cocoa applications? Is there a better way?
Indeed, +initialize won't be called for pure-Ruby classes. I recommend to use the Ruby idiom here, it's simpler :) Laurent
On 2010-10-10, at 17:24 , Laurent Sansonetti wrote:
Indeed, +initialize won't be called for pure-Ruby classes. I recommend to use the Ruby idiom here, it's simpler :)
So, the problem I'm seeing is what I alluded to earlier: writing stuff in the class body is more like +load than +initialize. Especially, not all classes may be loaded at that point. So I came up with this: http://gist.github.com/619585 Would it make sense to try to have something official for this functionality? In the process I noticed a few things: 1. You may see I opted for using _initialize instead of initialize for the method name. It turns out every ruby class in MacRuby already respond_to?(:initialize). That's not true in MRI. Is this a necessity of the Objective-C implementation? 2. It appears that the #singleton_class method does not exist in MacRuby. Oversight? File a ticket? 3. I initially tried implementing Object.inherited as: class Object @inherited = [] def self.inherited(m) @inherited << m end end This works in a standalone MacRuby file. In the context of an application, @inherited seems to be lost, and I get an error trying to call << on nil. What's going on here?
participants (3)
-
Caio Chassot
-
Laurent Sansonetti
-
Matt Aimonetti