Watson1978
12/24/2011 - 10:34 AM

MacRuby : blocks as property

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