Problems loading scripts using embedded Ruby interpreter

M

Matt Mower

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.=20
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 =3D #{$:}\"" );
// 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 =3D [NSBundle mainBundle];
NSString* scriptPath =3D [mainBundle pathForResource:mad:"cocoabug"
ofType:mad:"rb"];
NSString* scriptFolder =3D [scriptPath stringByDeletingLastPathComponen=
t];
NSString* operation =3D @"$: << \"";
operation =3D [operation stringByAppendingString:scriptFolder];
operation =3D [operation stringByAppendingString:mad:"\""];
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/Reso=
urces
Ruby =3D 1.8.4
Path =3D /Users/matt/Projects/RiC/build/Debug/RiC.app/Contents/Resource=
s
(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/Reso=
urces
Ruby =3D 1.8.4
Path =3D /Users/matt/Projects/RiC/build/Debug/RiC.app/Contents/Resource=
s
(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.=20
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,992
Messages
2,570,220
Members
46,807
Latest member
ryef

Latest Threads

Top