Skip to content

Instantly share code, notes, and snippets.

@Watson1978
Created December 24, 2011 10:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Watson1978/1517083 to your computer and use it in GitHub Desktop.
Save Watson1978/1517083 to your computer and use it in GitHub Desktop.
MacRuby : blocks as property
#import <Foundation/Foundation.h>
@interface TestBlocks : NSObject {
void (^func)(id);
}
@property (nonatomic, copy) void (^func)(id);
- (void)dealloc;
- (void)exec;
@end
#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;
}
require "mkmf"
$CFLAGS << ' -fobjc-gc '
create_makefile("Blocks")
require 'Blocks'
load_bridge_support_file 'Blocks.bridgesupport'
b = TestBlocks.new
b.func = Proc.new do |v|
p v
p "hello....."
end
b.exec
@Watson1978
Copy link
Author

$ gen_bridge_metadata -c '-I.' blocks.h > Blocks.bridgesupport
$ macruby extconf.rb
$ make
$ macruby test_blocks.rb 
"test"
"hello....."

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment