[MacRuby-devel] assign value to identifier in runtime with objc codes
Laurent Sansonetti
lsansonetti at apple.com
Sun Jul 26 22:50:34 PDT 2009
Hi Linan,
You can't really assign a Ruby local variable from Objective-C, since
Ruby locals are generally scoped around a particular method, but you
can assign to an instance variable of a given object, a constant, a
global variable (or eventually a class variable, but this is evil,
don't try that). Additionally, you can evaluate a local variable
assignment of a given Binding object from Objective-C, but this
becomes hardcore.
A simple example, setting an instance variable of an object created
from a Ruby class from Objective-C:
$ cat /tmp/t.m
#import <MacRuby/MacRuby.h>
int main(void)
{
[[MacRuby sharedRuntime] evaluateString:@"class Foo; def foo;
@foo; end; end"];
id obj = [NSClassFromString(@"Foo") new];
NSLog(@"-> %@", [obj performRubySelector:@selector(foo)]);
[obj performRubySelector:@selector(instance_variable_set:)
withArguments: @"@foo", @"hello", nil];
NSLog(@"-> %@", [obj performRubySelector:@selector(foo)]);
[obj performRubySelector:@selector(instance_variable_set:)
withArguments: @"@foo", @"world", nil];
NSLog(@"-> %@", [obj performRubySelector:@selector(foo)]);
}
$ gcc /tmp/t.m -o /tmp/t -fobjc-gc -framework MacRuby -framework
Foundation
$ /tmp/t
2009-07-26 22:48:29.366 t[53439:903] -> <null>
2009-07-26 22:48:29.368 t[53439:903] -> hello
2009-07-26 22:48:29.369 t[53439:903] -> world
HTH,
Laurent
On Jul 26, 2009, at 5:10 PM, Linan Wang wrote:
> Hi everybody,
> I'm trying to migrate from FScript to MacRuby. The problem I'm
> facing now is how to assign values to variables in the macruby
> runtime with objc codes. In Fscript framework, it's fairly
> straightforward: setObject:forIdentifier call of FSInterpreter.
> I've tried rb_define_varible but it does not work. seems it requires
> rb_objc_ocval_to_rval function call to convert the objc object to
> ruby object first. Any help is appreciated. Thanks
>
> --
> Best regards
>
> Linan Wang
> _______________________________________________
> MacRuby-devel mailing list
> MacRuby-devel at lists.macosforge.org
> http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
More information about the MacRuby-devel
mailing list