[MacRuby-devel] How do I subclass Obj-C classes in MacRuby?

russell muetzelfeldt russm-macruby-devel at slofith.org
Thu May 6 20:51:08 PDT 2010


> From: Thibault Martin-Lagardette <thibault.ml at 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 at 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 at 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> 




More information about the MacRuby-devel mailing list