[MacRuby] #1388: Can't subclass NSProxy
#1388: Can't subclass NSProxy ---------------------------+------------------------------------------------ Reporter: haxie1@… | Owner: lsansonetti@… Type: defect | Status: new Priority: blocker | Milestone: Component: MacRuby | Keywords: ---------------------------+------------------------------------------------ When trying to create a subclass of NSProxy, I get: `<main>': undefined method `inherited' for "#<Class:0x7fff7640e1f0>":String (NoMethodError) I am running the MacRuby nightly from September 13th and have the latest BridgeSupport installed. Example: {{{ class MyProxy < NSProxy def initWithTarget(target) @target = target end end str = "foo" proc = MyProxy.alloc.initWithTarget(str) }}} -- Ticket URL: <http://www.macruby.org/trac/ticket/1388> MacRuby <http://macruby.org/>
#1388: Can't subclass NSProxy ---------------------------+------------------------------------------------ Reporter: haxie1@… | Owner: lsansonetti@… Type: defect | Status: new Priority: blocker | Milestone: Component: MacRuby | Keywords: ---------------------------+------------------------------------------------ Comment(by mrada@…): In normal Ruby, the Class class has a no-op implementation of the inherited method so that the VM can always call the method on the superclasses of a new class even if you do not provide an implementation of your own. Since NSProxy does not live inside the standard inheritance hierarchy, it implements the NSObject protocol instead of subclassing NSObject, it does not get the methods that MacRuby has added to NSObject. The missing methods are inherent in how Ruby is implemented, like respond_to?. I'm not sure of the actual solution to this problem, but it will only happen for classes that do not inherit from NSObject. There are some workarounds, like checking if the object responds to the selector first, but the current system was designed to avoid that kind of overhead so I don't think that that is a good solution. Another workaround might be to define the class you are looking for in Objective-C, but you would also have to define the other methods that MacRuby expects objects to have. Someone else probably has better ideas, I am not too experienced with Objective-C. -- Ticket URL: <http://www.macruby.org/trac/ticket/1388#comment:2> MacRuby <http://macruby.org/>
#1388: Can't subclass NSProxy ---------------------------+------------------------------------------------ Reporter: haxie1@… | Owner: lsansonetti@… Type: defect | Status: new Priority: blocker | Milestone: Component: MacRuby | Keywords: ---------------------------+------------------------------------------------ Comment(by mrada@…): As a temporary workaround, you could just implement the required methods yourself. I've modified your sample code to make it work: {{{ class << NSProxy def method_added meth end def inherited klass end end class MyProxy < NSProxy def initWithTarget(target) @target = target end end str = "foo" proc = MyProxy.alloc.initWithTarget(str) }}} That snippet is enough to not throw an exception, but there are a number of other callbacks that Ruby makes that might throw exceptions and so you will have to implement them all yourself. -- Ticket URL: <http://www.macruby.org/trac/ticket/1388#comment:3> MacRuby <http://macruby.org/>
#1388: Can't subclass NSProxy ---------------------------+------------------------------------------------ Reporter: haxie1@… | Owner: lsansonetti@… Type: defect | Status: new Priority: blocker | Milestone: Component: MacRuby | Keywords: ---------------------------+------------------------------------------------ Comment(by mrada@…): I'm wondering now if this should be the recommended solution for objects that do not inherit from NSObject, or if MacRuby should try to handle this case itself... -- Ticket URL: <http://www.macruby.org/trac/ticket/1388#comment:4> MacRuby <http://macruby.org/>
#1388: Can't subclass NSProxy ---------------------------+------------------------------------------------ Reporter: haxie1@… | Owner: lsansonetti@… Type: defect | Status: new Priority: blocker | Milestone: Component: MacRuby | Keywords: ---------------------------+------------------------------------------------ Comment(by haxie1@…): Thanks for the response. For now I can just create my NSProxy subclass in Objc as part of a framework I am working on, but I do think that MacRuby should handle this on its own. Since MacRuby knows what methods it needs to call on NSProxy (or any object), it would make using this class as easy to use as it is on the ObjC if MR handled all of this. -- Ticket URL: <http://www.macruby.org/trac/ticket/1388#comment:5> MacRuby <http://macruby.org/>
participants (1)
-
MacRuby