# Novice ruby embed question

**URL:** <https://rubytalk.org/t/novice-ruby-embed-question/12013>\
**Category:** ruby-talk\
**Created:** [9 June 2004 08:26 UTC](https://rubytalk.org/t/novice-ruby-embed-question/12013 "2004-06-09T08:26:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bill\_Kelly](https://avatars.discourse-cdn.com/v4/letter/b/cc9497/32.png) [@Bill\_Kelly](https://rubytalk.org/u/Bill_Kelly)\
**Post date:** [9 June 2004 08:26 UTC](https://rubytalk.org/t/novice-ruby-embed-question/12013/1 "2004-06-09T08:26:08Z")

</div>

Hi,

I'm just getting started embedding ruby, have set up  
my main() as follows: (ala Pickaxe p.187, Embedding a  
Ruby Interpreter.)

{  
#ifdef \_WIN32  
&nbsp;&nbsp;&nbsp;&nbsp;// ruby win32 init  
&nbsp;&nbsp;&nbsp;&nbsp;NtInitialize(&argc, &argv);  
#endif

&nbsp;&nbsp;&nbsp;&nbsp;ruby\_init();  
&nbsp;&nbsp;&nbsp;&nbsp;ruby\_script("embedded");

&nbsp;&nbsp;&nbsp;&nbsp;rb\_load\_file("test.rb");

&nbsp;&nbsp;&nbsp;&nbsp;ruby\_run();  
}

This seems work if rb\_load\_file() succeeds... But if  
rb\_load\_file() fails to find the file, and tries to  
raise an exception, the longjmp() is crashing.

I'm wondering if I'd be better off replacing the  
rb\_load\_file() and ruby\_run() with a call to  
rb\_eval\_string("load test.rb") or some such?  
(rb\_eval\_string\_protect ??)

What I'd really like is to have ruby load and execute  
a particular ruby file ("test.rb"), and then not call exit()  
afterward. (But also not crash if file not found.)

Thanks for your help !

Regards,

Bill

---

<div class="post-metadata">

**Author:** ![Nobuyoshi\_Nakada](https://avatars.discourse-cdn.com/v4/letter/n/edb3f5/32.png) [@Nobuyoshi\_Nakada](https://rubytalk.org/u/Nobuyoshi_Nakada)\
**Post date:** [10 June 2004 02:06 UTC](https://rubytalk.org/t/novice-ruby-embed-question/12013/2 "2004-06-10T02:06:17Z")

</div>

Hi,

At Wed, 9 Jun 2004 17:26:08 +0900,  
Bill Kelly wrote in [ruby-talk:102909]:

> {

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int status;

> #ifdef \_WIN32  
> &nbsp;&nbsp;&nbsp;&nbsp;// ruby win32 init  
> &nbsp;&nbsp;&nbsp;&nbsp;NtInitialize(&argc, &argv);  
> #endif
> 
> &nbsp;&nbsp;&nbsp;&nbsp;ruby\_init();  
> &nbsp;&nbsp;&nbsp;&nbsp;ruby\_script("embedded");

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rb\_load\_protect(rb\_str\_new2("test.rb"), 0, &status);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (status == 0) {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ruby\_run();  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}

> **···**
>
> > }
> 
> --  
> Nobu Nakada

---

<div class="post-metadata">

**Author:** ![Bill\_Kelly](https://avatars.discourse-cdn.com/v4/letter/b/cc9497/32.png) [@Bill\_Kelly](https://rubytalk.org/u/Bill_Kelly)\
**Post date:** [10 June 2004 03:57 UTC](https://rubytalk.org/t/novice-ruby-embed-question/12013/3 "2004-06-10T03:57:43Z")

</div>

Hi Nobu,

> \> ruby\_init();  
> \> ruby\_script("embedded");  
> \>  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rb\_load\_protect(rb\_str\_new2("test.rb"), 0, &status);  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (status == 0) {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ruby\_run();  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}

Thanks !!!

I'd also like ruby not to call exit() as ruby\_run() does,  
when it's finished. Is it safe for me to just call  
ruby\_exec(), like:

&nbsp;&nbsp;&nbsp;&nbsp;rb\_load\_protect(rb\_str\_new2("test.rb"), 0, &status);  
&nbsp;&nbsp;&nbsp;&nbsp;if (status == 0) {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int state;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state = ruby\_exec();  
&nbsp;&nbsp;&nbsp;&nbsp;}

...instead of ruby\_run()? Is there a better way?

Thanks!

Regards,

Bill

> **···**
>
> From: \<nobu.nokada@softhome.net\>

---

<div class="post-metadata">

**Author:** ![Nobuyoshi\_Nakada](https://avatars.discourse-cdn.com/v4/letter/n/edb3f5/32.png) [@Nobuyoshi\_Nakada](https://rubytalk.org/u/Nobuyoshi_Nakada)\
**Post date:** [10 June 2004 05:02 UTC](https://rubytalk.org/t/novice-ruby-embed-question/12013/4 "2004-06-10T05:02:36Z")

</div>

Hi,

At Thu, 10 Jun 2004 12:57:43 +0900,  
Bill Kelly wrote in [ruby-talk:103030]:

> I'd also like ruby not to call exit() as ruby\_run() does,  
> when it's finished. Is it safe for me to just call  
> ruby\_exec(), like:
> 
> &nbsp;&nbsp;&nbsp;&nbsp;rb\_load\_protect(rb\_str\_new2("test.rb"), 0, &status);  
> &nbsp;&nbsp;&nbsp;&nbsp;if (status == 0) {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int state;  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state = ruby\_exec();  
> &nbsp;&nbsp;&nbsp;&nbsp;}
> 
> ...instead of ruby\_run()? Is there a better way?

Yes, it got provided for the sake of that usage.

> **···**
>
> --  
> Nobu Nakada
