[MacRuby-devel] Compiler directives

Laurent Sansonetti lsansonetti at apple.com
Sat May 24 13:46:45 PDT 2008


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);
      }
  }
+
+ at interface Protocol
+ at end
+
+ at implementation Protocol (MRFindProtocol)
++(id)protocolWithName:(NSString *)name
+{
+    return (id)objc_getProtocol([name UTF8String]);
+}
+ at end




More information about the MacRuby-devel mailing list