Hi folks,

I posted this message to ruby-talk yesterday but didn't receive any
responses. I hope you don't mind if I repost it here since I hope it
is more likely that people here have experience of embedding the ruby
interpreter and likely things that might be done wrong in the attempt.

I have embedded a Ruby 1.8.4 interpreter (compiled last night) into a
Cocoa application built using the latest version of the XCode
development tool on MacOSX. I embedded it by copying the
libruby-static.a file into the XCode project and marking it to be
included in the application bundle.

Using the instructions in the 2nd edition of Programming Ruby I have
managed to get the interpreter working and can execute Ruby statements
using rb_eval_string(). In particular by using ruby_init_loadpath and
rb_eval_string() to output $: I can be sure it is loaded the prepared
ruby and not the system ruby.

However I am having some problems when it comes to executing ruby
scripts using rb_load_file with a script included in the App bundle. I
don't think bundling is the issue since a bundle is just a directory
as far as the underlying OS (and Ruby) are concerned.

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 (as verified by printing it
us rb_eval_string( "puts $:" );. 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 (taking the discussion of rb_protect into
account) have terminated 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/