Created
October 10, 2011 20:19
-
-
Save alloy/1276398 to your computer and use it in GitHub Desktop.
MacRuby with Objective-C associated objects
This file contains hidden or 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> | |
#import <objc/runtime.h> | |
#import <MacRuby/MacRuby.h> | |
@interface NSObject (MRAssociatedObjects) | |
- (id)associatedObjectForSymbol:(NSString *)rubySymbolName; | |
- (void)setAssociatedObject:(id)value forSymbol:(NSString *)rubySymbolName; | |
@end | |
@implementation NSObject (MRAssociatedObjects) | |
static VALUE | |
symbolNameToSymbol(id rubySymbolName) | |
{ | |
VALUE sym; | |
if (SYMBOL_P(rubySymbolName)) { | |
sym = (VALUE)rubySymbolName; | |
} else if ([rubySymbolName isKindOfClass:[NSString class]]) { | |
sym = rb_name2sym([rubySymbolName UTF8String]); | |
} else { | |
[NSException raise:@"ArgumentError" | |
format:@"Expected a Symbol or NSString, but got `%@'", rubySymbolName]; | |
} | |
return sym; | |
} | |
- (id)associatedObjectForSymbol:(id)rubySymbolName | |
{ | |
return objc_getAssociatedObject(self, (void *)symbolNameToSymbol(rubySymbolName)); | |
} | |
- (void)setAssociatedObject:(id)value forSymbol:(id)rubySymbolName | |
{ | |
objc_setAssociatedObject(self, (void *)symbolNameToSymbol(rubySymbolName), value, OBJC_ASSOCIATION_RETAIN); | |
} | |
@end | |
// Ruby expects this | |
void | |
Init_associated_objects() {} | |
@interface TestAssociatedObject : NSObject | |
@end | |
@implementation TestAssociatedObject | |
- (void)computeAnswerInObjC | |
{ | |
if ([[self associatedObjectForSymbol:@"question"] isEqual:@"the answer to everything"]) { | |
[self setAssociatedObject:[NSNumber numberWithInt:42] forSymbol:@"computed_answer"]; | |
} | |
} | |
@end |
This file contains hidden or 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
% clang -fobjc-gc -framework Foundation -framework MacRuby -bundle -o associated_objects.bundle associated_objects.m | |
% macruby test.rb | |
42 | |
/Users/eloy/tmp/associated-objects/test.rb:6:in `[]=:': ArgumentError: Expected a Symbol or NSString, but got `42' (RuntimeError) | |
from /Users/eloy/tmp/associated-objects/test.rb:13:in `<main>' |
This file contains hidden or 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 "associated_objects" | |
class NSObject | |
alias_method :[], :associatedObjectForSymbol | |
def []=(symbol, value) | |
setAssociatedObject(value, forSymbol:symbol) | |
end | |
end | |
o = TestAssociatedObject.new | |
o[:question] = "the answer to everything" | |
o.computeAnswerInObjC | |
p o[:computed_answer] # => 42 | |
# anything other than a string/symbol makes it go boom | |
o["string is ok too"] = "yup" | |
o[42] = "ohnoes" # => test.rb:6:in `[]=:': ArgumentError: Expected a Symbol or NSString, but got `42' (RuntimeError) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment