Forum: Ruby Problems loading scripts using embedded Ruby interpreter

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Matt M. (Guest)
on 2006-01-25 02:09
(Received via mailing list)
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

Next I started modifying main.m adding:

  ruby_script( "CocoaBug" );
  ruby_eval_string( "puts \"Path = #{$:}\"" );
  // rb_load_file( fileName );

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

    NSBundle* mainBundle = [NSBundle mainBundle];
    NSString* scriptPath = [mainBundle pathForResource:@"cocoabug"
    NSString* scriptFolder = [scriptPath
    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" )
    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:
    Ruby = 1.8.4
    Path =
    (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:
    Ruby = 1.8.4
    Path =
    (eval): No such file or directory -- cocoabug (LoadError)

However attempting the same thing from IRB using:

    $: <<
    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 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:

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


This topic is locked and can not be replied to.