From: Laurent S. <lsa...@ap...> - 2007-08-28 23:18:20
|
Hi, On Aug 28, 2007, at 10:19 PM, sergio wrote: > Hi all, > > this is my first post to this list. Welcome! > [...] > /Library/Frameworks/RubyCocoa.framework/Versions/A/Resources/ruby/osx/ > objc/oc_import.rb:318:in `_ns_behavior_method_added': Cannot override > Objective-C method 'awakeFromInsert' with Ruby method > #awakeFromInsert, they should both have the same number of arguments. > (expected arity 0, got -1) (RuntimeError) > > I am getting this behaviour in RubyCocoa 0.12.0 and 0.11.1. The same > code works under RubyCocoa 0.10.1 (and previous versions that used > the ns_override directive). Furthermore, this behaviour only is shown > when overriding methods that take no parameters; everything works > fine when overriding, say, willChangeValueForKey, which takes one > parameter. I tried to override the didSave method as well (no > parameters), and it also failed, so it seems to be related to the > number of parameters. > > Of course, overriding always works when I am not doing it via > define_method but plainly in the class definition. > > Any clues anyone? I am willing to explore further this issue on my > own, provided there is no easy explanation, if someone is so kind to > point me in the right direction.... The exception you get was added to avoid users overriding methods with the wrong arity (which could cause crashes when calling back the Ruby method from ObjC). The problem here is that an empty block (proc {}) has an arity of -1, for an unknown reason. But a block with an empty argument list (proc {||}) will have an arity of 0. So normally if you call define_method with blocks with an empty argument list, you should not get the exception. Typically, we don't really care about methods with a -1 arity, since they are usually not written accidentally. So we could just not throw the exception for -1 arity methods. Laurent |