Ruby-Tk with embedded interpreter on OS X

Is it possible to run Ruby-Tk from a Ruby interpreter embedded in C?

My use case is a Ruby app for the Mac App Store. Because Ruby does not
have any usable deployment tools for desktop apps on the Mac, I was
hoping to deploy a simple stub app with an embedded interpreter that
would link against the system-installed Ruby 2.0 (this is on 10.9,
Mavericks).

I’ve put together a simple test case, which crashes with a segmentation
fault. I’m not clear why. Here is my C code:

#include <ruby/ruby.h>

int main()
{
ruby_init();
char* file = “/Users/kevin/Desktop/test.rb”;
ruby_script(file);
ruby_init_loadpath();
void *node = rb_load_file(file);
ruby_run_node(node);
return 0;
}

And here is my test script:

require ‘tk’

root = TkRoot.new { title “Hello, World!” }
TkLabel.new(root) do
text ‘Hello, World!’
pack { padx 15 ; pady 15; side ‘left’ }
end
Tk.mainloop

Pretty simple stuff.

Here is a bit of debugging output:

/Users/kevin/Desktop/test.rb: [BUG] Segmentation fault
ruby 2.0.0p451 (2014-02-24 revision 45167) [universal.x86_64-darwin13]

Application Specific Information:
abort() called

Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0 libsystem_kernel.dylib 0x00007fff87119866 __pthread_kill +
10
1 libsystem_pthread.dylib 0x00007fff8fb8f35c pthread_kill + 92
2 libsystem_c.dylib 0x00007fff90df8b1a abort + 125
3 libruby.2.0.0.dylib 0x000000010ab90d19 rb_bug + 184
4 libruby.2.0.0.dylib 0x000000010ac25e9e 0x10ab67000 +
781982
5 libsystem_platform.dylib 0x00007fff8995c5aa _sigtramp + 26
6 ??? 000000000000000000 0 + 0
7 libruby.2.0.0.dylib 0x000000010ac78b87 rb_iseq_eval_main

  • 95
    8 libruby.2.0.0.dylib 0x000000010ab959b0 0x10ab67000 +
    190896
    9 libruby.2.0.0.dylib 0x000000010ab958f2 ruby_run_node +
    47
    10 test 0x000000010ab61f03 main + 67
    11 libdyld.dylib 0x00007fff925a25fd start + 1

And here is how I compiled my stub app:

gcc -Wall -v -o test test.c -lruby

Is what I’m trying to do possible, or is it a fool’s errand? If this can
be done, can anyone point me in the right direction about how to
structure my C code?

Thanks,
Kevin

On 4/25/14, 5:26 PM, Kevin W. wrote:

Is what I’m trying to do possible, or is it a fool’s errand? If this can
be done, can anyone point me in the right direction about how to
structure my C code?

Turns out this code seems to work well:

#include <ruby/ruby.h>

int main()
{
ruby_init();

char* file = “/Users/kevin/Desktop/test.rb”;

ruby_init_loadpath();

ruby_script(file);

char* options[] = { “”, file, };

void* node = ruby_options( 2, options );

return ruby_run_node( node );

}

Thanks to the responders at https://www.ruby-forum.com/topic/215431 for
showing the way here. This code launches my Ruby-Tk code very nicely and
does so with an embedded interpreter, rather than launching a separate
Ruby process. This will make deploying a Ruby-Tk app on the Mac much
simpler.

–Kevin