# Problems loading scripts using embedded Ruby interpreter

**URL:** <https://www.ruby-forum.com/t/problems-loading-scripts-using-embedded-ruby-interpreter/96935>\
**Category:** Ruby\
**Created:** [January 25, 2006, 1:09am UTC](https://www.ruby-forum.com/t/problems-loading-scripts-using-embedded-ruby-interpreter/96935 "2006-01-25T01:09:48Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![mattmcswain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@mattmcswain](https://www.ruby-forum.com/u/mattmcswain)\
**Post date:** [January 25, 2006, 1:09am UTC](https://www.ruby-forum.com/t/problems-loading-scripts-using-embedded-ruby-interpreter/96935/1 "2006-01-25T01:09:48Z")

</div>

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:  
[blogs.it](http://matt.blogs.it/share/source/main.txt)

If anyone could help me out I’d be much obliged.

Regards,

Matt
