[MacRuby-devel] assign value to identifier in runtime with objc codes

Linan Wang tali.wang at gmail.com
Mon Jul 27 13:01:28 PDT 2009


Got it. thanks!
On Mon, Jul 27, 2009 at 7:50 PM, Laurent Sansonetti
<lsansonetti at apple.com>wrote:

> Hi Linan,
> You can evaluate "self" for that.
>
> id ruby_self = [[MacRuby sharedRuntime] evaluateString:@"self"];
>
> Laurent
>
> On Jul 27, 2009, at 1:52 AM, Linan Wang wrote:
>
> Hi Laurent,Thanks for the explanation.
> A related question: how to get the "main/self" object of the macruby
> runtime?
>
> On Mon, Jul 27, 2009 at 6:50 AM, Laurent Sansonetti <lsansonetti at apple.com
> > wrote:
>
>> 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
>>>
>>
>> _______________________________________________
>> MacRuby-devel mailing list
>> MacRuby-devel at lists.macosforge.org
>> http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
>>
>
>
>
> --
> Best regards
>
> Linan Wang
>  _______________________________________________
> MacRuby-devel mailing list
> MacRuby-devel at lists.macosforge.org
> http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
>
>
>
> _______________________________________________
> MacRuby-devel mailing list
> MacRuby-devel at lists.macosforge.org
> http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
>
>


-- 
Best regards

Linan Wang
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-devel/attachments/20090727/3dd9b6e5/attachment-0001.html>


More information about the MacRuby-devel mailing list