How would I turn this ObjC into MacRuby? [ MyClass conformsToProtocol:@protocol( MyProtocol ) ]; Thanks, Zach
Hi Zach, On May 24, 2008, at 1:22 PM, Zach Holt wrote:
How would I turn this ObjC into MacRuby?
[ MyClass conformsToProtocol:@protocol( MyProtocol ) ];
Currently this is not possible (same in RubyCocoa), but with the enclosed patch: $ ./miniruby -e "framework 'Foundation'; p 'foo'.conformsToProtocol(Protocol.protocolWithName('NSObject'))" true $ ./miniruby -e "framework 'Foundation'; p 'foo'.conformsToProtocol(Protocol.protocolWithName('NSDoesNotExist'))" false Any idea regarding a shortcut? (Protocol.protocolWithName is too long). For example we could automatically convert symbols to Protocol objects. Also, calling #inspect on a Protocol object currently crashes (because it's not NSObject-based, where inspect is defined). Laurent Index: objc.m =================================================================== --- objc.m (revision 216) +++ objc.m (working copy) @@ -2914,3 +2914,13 @@ CFRunLoopAddTimer(CFRunLoopGetMain(), timer, kCFRunLoopDefaultMode); } } + +@interface Protocol +@end + +@implementation Protocol (MRFindProtocol) ++(id)protocolWithName:(NSString *)name +{ + return (id)objc_getProtocol([name UTF8String]); +} +@end
On May 24, 2008, at 1:46 PM, Laurent Sansonetti wrote:
Currently this is not possible (same in RubyCocoa), but with the enclosed patch:
I'll try it out.
Any idea regarding a shortcut? (Protocol.protocolWithName is too long). For example we could automatically convert symbols to Protocol objects.
Protocol[:ProtocolName] Protocol.named( 'ProtocolName' ) :ProtocolName.to_protocol 'ProtocolName'.to_protocol With the first one, you could alias :conformsToProtocol to :conformsTo, and have it still read somewhat like the ObjC. obj.conformsTo( Protocol[:ProtocolName] )
Also, calling #inspect on a Protocol object currently crashes (because it's not NSObject-based, where inspect is defined).
Thanks for the tip.
participants (2)
-
Laurent Sansonetti
-
Zach Holt