Glad it works :-) The reason why your example doesn't work is because in MacRuby, String descends from NSString, which is a Foundation object. Foundation objects use "init" and not "initalize", and for syntaxic sugar reasons, #new is aliased to [[obj alloc] init] on Foundation objects :-). $> macirb --simple-prompt
class A < String def initialize super puts "initialize" end def init if super puts "init" self end end end => nil A.new init => ""
On non-Foundation objects, #new behaves as you expect (the ruby way) $> macirb --simple-prompt
class A < Range def initialize(a, b) super puts "range initialize!" end end => nil A.new(1, 2) range initialize! => 1..2
I agree this difference can be confusing, and I think it could find its place in a certain book *stares at Matt* -- Thibault Martin-Lagardette On May 6, 2010, at 20:51, russell muetzelfeldt wrote:
From: Thibault Martin-Lagardette <thibault.ml@gmail.com>
What you can do is a "factory" just like this:
...
You can also do a more Cocoa-ish way:
...
Hope that helps!
yep, that all works... I'd rather be doing it in a more Ruby-ish way though... :)
Is the fact that the Ruby-standard initialize method is not called for Ruby classes descended from Obj-C classes expected? To me, at least, it violates the principle of least surprise...
for example, in ruby 1.9.1 we see initialize being called
russm@worcestershire:~$ irb1.9.1 irb(main):001:0> class C2 < String irb(main):002:1> attr_accessor :foo irb(main):003:1> def initialize foo = "default2" irb(main):004:2> @foo = foo irb(main):005:2> STDERR.puts "setting #{foo}" irb(main):006:2> end irb(main):007:1> end => nil irb(main):008:0> c2 = C2.new setting default2 => "" irb(main):009:0>
but not in macruby 0.6
russm@alcazar:~$ macirb irb(main):001:0> class C2 < String irb(main):002:1> attr_accessor :foo irb(main):003:1> def initialize foo = "default2" irb(main):004:2> @foo = foo irb(main):005:2> STDERR.puts "setting #{foo}" irb(main):006:2> end irb(main):007:1> end => nil irb(main):008:0> c2 = C2.new => "" irb(main):009:0>
_______________________________________________ MacRuby-devel mailing list MacRuby-devel@lists.macosforge.org http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel