Created
December 24, 2011 10:34
-
-
Save Watson1978/1517083 to your computer and use it in GitHub Desktop.
MacRuby : blocks as property
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
@interface TestBlocks : NSObject { | |
void (^func)(id); | |
} | |
@property (nonatomic, copy) void (^func)(id); | |
- (void)dealloc; | |
- (void)exec; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "blocks.h" | |
@implementation TestBlocks | |
@synthesize func; | |
- (void)dealloc | |
{ | |
[func release]; | |
[super dealloc]; | |
} | |
- (void)exec { | |
if (self.func) { | |
self.func(@"test"); | |
} | |
} | |
@end | |
// For MacRuby | |
void Init_Blocks(void) {} | |
// gcc blocks.m -framework Foundation | |
// ./a.out | |
int main(void) | |
{ | |
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; | |
TestBlocks *block = [[TestBlocks alloc] init]; | |
void (^func)(id) = ^(id v){ | |
NSLog(@"%@", v); | |
NSLog(@"hello....."); | |
}; | |
block.func = func; | |
[block exec]; | |
[pool release]; | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "mkmf" | |
$CFLAGS << ' -fobjc-gc ' | |
create_makefile("Blocks") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'Blocks' | |
load_bridge_support_file 'Blocks.bridgesupport' | |
b = TestBlocks.new | |
b.func = Proc.new do |v| | |
p v | |
p "hello....." | |
end | |
b.exec |
Author
Watson1978
commented
Dec 24, 2011
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment