[MacRuby-devel] MacRuby 0.5b1: Reloading classes at runtime

Laurent Sansonetti lsansonetti at apple.com
Sat Nov 14 18:51:57 PST 2009


Hi Frank,

Sorry for the late response. It seems to work here. Following is a  
small test that loads a class from a Ruby file, calls +new on it, then  
changes the code, re-loads it again.

Could you try to reduce your problem to a small test like this one and  
let us know?

Thanks,
Laurent

$ pwd
/tmp/test
$ cat t.m
#import <Foundation/Foundation.h>
#import <MacRuby/MacRuby.h>

int main(void)
{
     MacRuby *runtime = [MacRuby sharedRuntime];

     [@"class Foo; def initialize; p 123; end; end" writeToFile:@"/tmp/ 
test/test.rb" atomically:YES encoding:NSUTF8StringEncoding error:nil];
     [runtime evaluateString:[NSString stringWithFormat:@"load \"/tmp/ 
test/test.rb\""]];
     [NSClassFromString(@"Foo") new];

     [@"class Foo; def initialize; p 456; end; end" writeToFile:@"/tmp/ 
test/test.rb" atomically:YES encoding:NSUTF8StringEncoding error:nil];
     [runtime evaluateString:[NSString stringWithFormat:@"load \"/tmp/ 
test/test.rb\""]];
     [NSClassFromString(@"Foo") new];

     return 0;
}
$ gcc t.m -o t -fobjc-gc -framework MacRuby -framework Foundation
$ ./t
123
456

On Nov 12, 2009, at 4:03 PM, Frank Illenberger wrote:

> Thanks for your quick answer. But sadly, it does not seem to work.
>
> My application is a regular Objective-C application and uses MacRuby  
> to load plug-in ruby classes which reside in their own .rb file.
> First I implemented the loading like this:
>
> NSString* path = @"/path/to/MyPlugInClass.rb";
> [[MacRuby sharedRuntime] evaluateFileAtPath: path]
> id plugInInstance = [[NSClassFromString(@"MyPlugInClass") alloc]  
> init];
>
> This is working fine when the class is loaded for the first time.  
> Reloading the class while the app keeps running after the .rb has  
> been edited however does not refresh the class.
>
> Then I tried to use the "load" command as you suggested:
>
> NSString* path = @"/path/to/MyPlugInClass.rb";
> NSString* command = [NSString stringWithFormat:@"load '%@'", path];
> [[MacRuby sharedRuntime] evaluateString: command]
> id plugInInstance = [[NSClassFromString(@"MyPlugInClass") alloc]  
> init];
>
> But it shows the same behavior as the first case.
> Am I doing something wrong here?
>
> Cheers
>
> Frank
>
> Am 12.11.2009 um 20:40 schrieb Matt Aimonetti:
>
>> This is the right place to ask this kind of questions and welcome  
>> to the list :)
>>
>> What kind of file are you loading? If you are reloading a ruby, you  
>> might want to try to use load instead of require.
>> http://www.fromjavatoruby.com/2008/10/require-vs-load.html
>>
>> - Matt
>>
>>
>> On Thu, Nov 12, 2009 at 11:11 AM, Frank Illenberger <illenberger at mac.com 
>> > wrote:
>> Hello folks,
>>
>> please excuse me if this is not the right mailing list to post this  
>> question:
>>
>> I am currently porting an application from PyObjC to MacRuby. Under  
>> PyObjC it is possible to reload a .py file containing a class  
>> definition at runtime. -[MacRuby evaluateFileAtPath:]  works fine  
>> for me loading a class for the first time. But after is loaded, it  
>> is not updated when called again. Does anybody know if there is a  
>> way to make this work with MacRuby?
>>
>> Thanks and cheers
>>
>> Frank
>
> _______________________________________________
> 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