From: Matt Mower Date: 2006-01-25T09:08:38+09:00 Subject: Problems loading scripts using embedded Ruby interpreter Hi folks, I am trying to embed the Ruby interpreter into a Cocoa application and am having some problems. I hope I can find some help here. My aim is to embed a specific Ruby interpreter into the Cocoa application (i.e. the ruby interpreter & any necessary files should be in the app bundle) and execute a script from within the bundle. In XCode 2.2. (latest version) I have created a Cocoa application. Then from a fresh ruby-1.8.4 i have copied the libruby-static.a into the Resources folder of my project and told XCode to copy it to the app. Next I started modifying main.m adding: ruby_init(); ruby_script( "CocoaBug" ); ruby_eval_string( "puts \"Path = #{$:}\"" ); // rb_load_file( fileName ); ruby_finalize(); However, although the Ruby interpreter seems to work fine, all is not well. The application correctly prints the contents of $: but I cannot get it to load the file through rb_load_file. If I try to preload $: with the path to the Ruby scripts in my app bundle NSBundle* mainBundle = [NSBundle mainBundle]; NSString* scriptPath = [mainBundle pathForResource:@"cocoabug" ofType:@"rb"]; NSString* scriptFolder = [scriptPath stringByDeletingLastPathComponent]; NSString* operation = @"$: << \""; operation = [operation stringByAppendingString:scriptFolder]; operation = [operation stringByAppendingString:@"\""]; rb_eval_string( [operation fileSystemRepresentation] ); This code works and sets the $: correctly. However attempts to do rb_load_file( "cocoabug" ) or rb_load_file( "cocoabug.rb" ) Both raise load errors. [Session started at 2006-01-24 21:52:29 +0000.] Load script: cocoabug.rb From Folder: /Users/matt/Projects/RiC/build/Debug/RiC.app/Contents/Resources Ruby = 1.8.4 Path = /Users/matt/Projects/RiC/build/Debug/RiC.app/Contents/Resources (eval): No such file or directory -- cocoabug.rb (LoadError) RiC has exited with status -1. [Session started at 2006-01-24 21:52:46 +0000.] Load script: cocoabug From Folder: /Users/matt/Projects/RiC/build/Debug/RiC.app/Contents/Resources Ruby = 1.8.4 Path = /Users/matt/Projects/RiC/build/Debug/RiC.app/Contents/Resources (eval): No such file or directory -- cocoabug (LoadError) However attempting the same thing from IRB using: $:.clear $: << "/Users/matt/Projects/RiC/build/Debug/RiC.app/Contents/Resources" require "cocoabug" Works fine. The alternative approach that I tried was to load the file using the complete path, i.e. rb_load_file( [scriptPath fileSystemRepresentation] ); Does not raise a LoadError, but doesn't appear to work either. Whatever I put in cocoabug.rb does not get executed including raising Exception.new which should terminate the whole process. I'm kinda stumped as to what's wrong but I'm so new to Cocoa/XCode and the inner workings of the Ruby interpreter that I'm not sure where to look next for the problem. The source code to my app is available from: http://matt.blogs.it/share/source/main.txt If anyone could help me out I'd be much obliged. Regards, Matt -- Matt Mower :: http://matt.blogs.it/