# How to Load Script from a C Extension?

**URL:** <https://rubytalk.org/t/how-to-load-script-from-a-c-extension/973>\
**Category:** ruby-talk\
**Created:** [2 August 2002 16:14 UTC](https://rubytalk.org/t/how-to-load-script-from-a-c-extension/973 "2002-08-02T16:14:34Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![William\_Djaja\_Tjokr1](https://avatars.discourse-cdn.com/v4/letter/w/87869e/32.png) [@William\_Djaja\_Tjokr1](https://rubytalk.org/u/William_Djaja_Tjokr1)\
**Post date:** [2 August 2002 16:14 UTC](https://rubytalk.org/t/how-to-load-script-from-a-c-extension/973/1 "2002-08-02T16:14:34Z")

</div>

Hi,

Is it possible to load (and execute) a Ruby script from within a C  
extension? From my Ruby code, I can just write

```
.....
load 'conf.rb'
.....

```

and my configuration parameters are loaded at that point. However, from a  
C extension, when I wrote

```
.....
rb_load (rb_str_new2 ("conf.rb"), 0);
.....

```

the program crashed. The strange thins is, if I moved the code several  
lines up, the program is OK, but it is not its correct place as the  
configuration parameters will be rewritten by the defaults in the  
extension itself.

Regards,

Bill

---

<div class="post-metadata">

**Author:** ![William\_Djaja\_Tjokr1](https://avatars.discourse-cdn.com/v4/letter/w/87869e/32.png) [@William\_Djaja\_Tjokr1](https://rubytalk.org/u/William_Djaja_Tjokr1)\
**Post date:** [2 August 2002 17:54 UTC](https://rubytalk.org/t/how-to-load-script-from-a-c-extension/973/2 "2002-08-02T17:54:42Z")

</div>

Hi,

My current temporary solution is to open the Ruby script file in C, get it  
line by line with “fgets”, and invoke “rb\_eval\_string” for each line. The  
only problem is that it does not work with multi-line Ruby statements. Is  
there a better (and easier) solution?

Regards,

Bill

> **···**
>
> ==============================================================================  
> William Djaja Tjokroaminata [billtj@z.glue.umd.edu](mailto:billtj@z.glue.umd.edu) wrote:
> 
> > Hi,
> 
> > Is it possible to load (and execute) a Ruby script from within a C  
> > extension? From my Ruby code, I can just write
> 
> > ```
> > .....
> > load 'conf.rb'
> > .....
> > 
> > ```

---

<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:** [2 August 2002 18:15 UTC](https://rubytalk.org/t/how-to-load-script-from-a-c-extension/973/3 "2002-08-02T18:15:52Z")

</div>

Hi,

> **···**
>
> At Sat, 3 Aug 2002 01:14:34 +0900, William Djaja Tjokroaminata wrote:
> 
> > ```
> > .....
> > rb_load (rb_str_new2 ("conf.rb"), 0);
> > .....
> > 
> > ```
> > 
> > the program crashed. The strange thins is, if I moved the code several  
> > lines up, the program is OK, but it is not its correct place as the  
> > configuration parameters will be rewritten by the defaults in the  
> > extension itself.
> 
> Perhaps, the code in that several lines is something wrong.
> 
> –  
> Nobu Nakada

---

<div class="post-metadata">

**Author:** ![Alv](https://avatars.discourse-cdn.com/v4/letter/a/eada6e/32.png) [@Alv](https://rubytalk.org/u/Alv)\
**Post date:** [3 August 2002 20:56 UTC](https://rubytalk.org/t/how-to-load-script-from-a-c-extension/973/4 "2002-08-03T20:56:16Z")

</div>

William Djaja Tjokroaminata [billtj@z.glue.umd.edu](mailto:billtj@z.glue.umd.edu) wrote in message news:[aieb3t$o97$1@grapevine.wam.umd.edu](mailto:aieb3t$o97$1@grapevine.wam.umd.edu)…

> Hi,
> 
> Is it possible to load (and execute) a Ruby script from within a C  
> extension? From my Ruby code, I can just write
> 
> ```
> .....
> load 'conf.rb'
> .....
> 
> ```
> 
> and my configuration parameters are loaded at that point. However, from a  
> C extension, when I wrote
> 
> ```
> .....
> rb_load (rb_str_new2 ("conf.rb"), 0);
> .....
> 
> ```

rb\_require(“conf.rb”);

works for me

Tomek ‘Alva’ Wroblewski

---

<div class="post-metadata">

**Author:** ![William\_Djaja\_Tjokr1](https://avatars.discourse-cdn.com/v4/letter/w/87869e/32.png) [@William\_Djaja\_Tjokr1](https://rubytalk.org/u/William_Djaja_Tjokr1)\
**Post date:** [2 August 2002 18:34 UTC](https://rubytalk.org/t/how-to-load-script-from-a-c-extension/973/5 "2002-08-02T18:34:52Z")

</div>

Hi,

I checked the code and it appears that all those lines are just global  
variable definitions. Also, when I open the file manually (in C) and just  
run “rb\_eval\_string” on each line, the program is fine.

(At least, “rb\_load” is really the correct function that I need?)

Regards,

Bill

> **···**
>
> ===========================================================================  
> [nobu.nokada@softhome.net](mailto:nobu.nokada@softhome.net) wrote:
> 
> > > ```
> > > .....
> > > rb_load (rb_str_new2 ("conf.rb"), 0);
> > > .....
> > > 
> > > ```
> > > 
> > > the program crashed. The strange thins is, if I moved the code several  
> > > lines up, the program is OK, but it is not its correct place as the  
> > > configuration parameters will be rewritten by the defaults in the  
> > > extension itself.
> 
> > Perhaps, the code in that several lines is something wrong.
> 
> > –  
> > Nobu Nakada

---

<div class="post-metadata">

**Author:** ![Ptkwt1](https://avatars.discourse-cdn.com/v4/letter/p/85e7bf/32.png) [@Ptkwt1](https://rubytalk.org/u/Ptkwt1)\
**Post date:** [3 August 2002 06:55 UTC](https://rubytalk.org/t/how-to-load-script-from-a-c-extension/973/6 "2002-08-03T06:55:40Z")

</div>

Why call Ruby from the C side? It seems to me to be easier to call your C  
code from Ruby. Let Ruby drive…  
See my post: embed or swig? for more info…

(OK, I guess if you have to distribute an executable that would be a  
reason to embed Ruby in your app instead of using Ruby to call your C  
code)

Phil

In article [aiegkr$qin$1@grapevine.wam.umd.edu](mailto:aiegkr$qin$1@grapevine.wam.umd.edu),

> **···**
>
> William Djaja Tjokroaminata [billtj@z.glue.umd.edu](mailto:billtj@z.glue.umd.edu) wrote:
> 
> > Hi,
> > 
> > My current temporary solution is to open the Ruby script file in C, get it  
> > line by line with “fgets”, and invoke “rb\_eval\_string” for each line. The  
> > only problem is that it does not work with multi-line Ruby statements. Is  
> > there a better (and easier) solution?
> > 
> > Regards,
> > 
> > # Bill
> > 
> > William Djaja Tjokroaminata [billtj@z.glue.umd.edu](mailto:billtj@z.glue.umd.edu) wrote:
> > 
> > > Hi,
> > 
> > > Is it possible to load (and execute) a Ruby script from within a C  
> > > extension? From my Ruby code, I can just write
> > 
> > > ```
> > > .....
> > > load 'conf.rb'
> > > .....
> > > 
> > > ```

---

<div class="post-metadata">

**Author:** ![William\_Djaja\_Tjokro](https://avatars.discourse-cdn.com/v4/letter/w/c89c15/32.png) [@William\_Djaja\_Tjokro](https://rubytalk.org/u/William_Djaja_Tjokro)\
**Post date:** [5 August 2002 13:58 UTC](https://rubytalk.org/t/how-to-load-script-from-a-c-extension/973/7 "2002-08-05T13:58:57Z")

</div>

Hi,

Yes, in my C extension Init\_xxx function, I have a rb\_require right after  
the variable declarations and it works. Then, I have some other C codes  
such as class definitions. When I have another rb\_require, it resulted in  
segmentation fault.

Is it an implied rule that in a C extension all the rb\_require’s should be  
put at the beginning of the Init function? If rb\_require does not work,  
should rb\_load or rb\_load\_protect work in the middle of the Init  
function? At least in my case using rb\_eval\_string it works, but it  
cannot handle multi-line Ruby statements. Thanks.

Regards,

Bill

> **···**
>
> ===========================================================================  
> Alva [alv@poczta.onet.pl](mailto:alv@poczta.onet.pl) wrote:
> 
> > rb\_require(“conf.rb”);
> 
> > works for me
> 
> > Tomek ‘Alva’ Wroblewski

---

<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:** [3 August 2002 01:16 UTC](https://rubytalk.org/t/how-to-load-script-from-a-c-extension/973/8 "2002-08-03T01:16:25Z")

</div>

Hi,

> I checked the code and it appears that all those lines are just global  
> variable definitions. Also, when I open the file manually (in C) and just  
> run “rb\_eval\_string” on each line, the program is fine.

Could you show me the code fragment and backtrace? And what’s  
your environment; platform, ruby version and so on?

> (At least, “rb\_load” is really the correct function that I need?)

Yes.

> **···**
>
> At Sat, 3 Aug 2002 03:34:52 +0900, William Djaja Tjokroaminata wrote:
> 
> –  
> Nobu Nakada

---

<div class="post-metadata">

**Author:** ![ts1](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@ts1](https://rubytalk.org/u/ts1)\
**Post date:** [3 August 2002 11:21 UTC](https://rubytalk.org/t/how-to-load-script-from-a-c-extension/973/9 "2002-08-03T11:21:37Z")

</div>

> (At least, "rb\_load" is really the correct function that I need?)

It's best to use rb\_load\_protect(), this give you the possibility to catch  
errors given by ruby.

Guy Decoux

---

<div class="post-metadata">

**Author:** ![ts1](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@ts1](https://rubytalk.org/u/ts1)\
**Post date:** [3 August 2002 12:24 UTC](https://rubytalk.org/t/how-to-load-script-from-a-c-extension/973/10 "2002-08-03T12:24:28Z")

</div>

> Why call Ruby from the C side? It seems to me to be easier to call your C  
> code from Ruby. Let Ruby drive...  
> See my post: embed or swig? for more info...

Well, probably it exist different problems with different solutions 🙂

For example, just try to use swig to write PL/Ruby 🙂

Guy Decoux

---

<div class="post-metadata">

**Author:** ![William\_Djaja\_Tjokro](https://avatars.discourse-cdn.com/v4/letter/w/c89c15/32.png) [@William\_Djaja\_Tjokro](https://rubytalk.org/u/William_Djaja_Tjokro)\
**Post date:** [5 August 2002 13:18 UTC](https://rubytalk.org/t/how-to-load-script-from-a-c-extension/973/11 "2002-08-05T13:18:49Z")

</div>

Hi,

Yes, in my application, initially it is a Ruby script that calls my C  
extension, as a user will run it as

```
ruby my_script.rb

```

and in “my\_script.rb” the user has to put “require  
‘my\_extension’”. However, the configuration file is written in Ruby,  
which is invoked by my\_extension. It is this part which is not working.

Is there another solution to this ruby-\>C-\>ruby file sequence problem?

Regards,

Bill

> **···**
>
> ============================================================================  
> Phil Tomson [ptkwt@shell1.aracnet.com](mailto:ptkwt@shell1.aracnet.com) wrote:
> 
> > Why call Ruby from the C side? It seems to me to be easier to call your C  
> > code from Ruby. Let Ruby drive…  
> > See my post: embed or swig? for more info…
> 
> > (OK, I guess if you have to distribute an executable that would be a  
> > reason to embed Ruby in your app instead of using Ruby to call your C  
> > code)
> 
> > Phil

---

<div class="post-metadata">

**Author:** ![ts1](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@ts1](https://rubytalk.org/u/ts1)\
**Post date:** [5 August 2002 14:05 UTC](https://rubytalk.org/t/how-to-load-script-from-a-c-extension/973/12 "2002-08-05T14:05:28Z")

</div>

> Yes, in my C extension Init\_xxx function, I have a rb\_require right after  
> the variable declarations and it works. Then, I have some other C codes  
> such as class definitions. When I have another rb\_require, it resulted in  
> segmentation fault.

You have a bug. Post your source or nobody will be able to help you

Guy Decoux

---

<div class="post-metadata">

**Author:** ![William\_Djaja\_Tjokro](https://avatars.discourse-cdn.com/v4/letter/w/c89c15/32.png) [@William\_Djaja\_Tjokro](https://rubytalk.org/u/William_Djaja_Tjokro)\
**Post date:** [5 August 2002 13:38 UTC](https://rubytalk.org/t/how-to-load-script-from-a-c-extension/973/13 "2002-08-05T13:38:52Z")

</div>

Hi,

My environment is  
ruby -v : ruby 1.6.7 (2002-03-01) [i686-linux]  
uname -a: Linux 2.4.8-26mdk #1 Sun Sep 23 17:06:39 CEST  
2001 i686 unknown

Before I try to put the code fragment, what is exactly the second argument  
in “rb\_load (value, int)”? Can I simply set the int to zero? Also, what  
is the third argument in “rb\_load\_protect (value, int, int\*)”?

For your information, in my case the sequence of execution is like  
this. A script is run in Ruby, such as “ruby my\_script.rb”. my\_script\_rb  
has “require ‘my\_extension’”. my\_extension.c has “rb\_load” inside its  
“Init\_xxx” function. Thanks.

Regards,

Bill

> **···**
>
> ===========================================================================  
> [nobu.nokada@softhome.net](mailto:nobu.nokada@softhome.net) wrote:
> 
> > Could you show me the code fragment and backtrace? And what’s  
> > your environment; platform, ruby version and so on?
> 
> > > (At least, “rb\_load” is really the correct function that I need?)
> 
> > Yes.
> 
> > –  
> > Nobu Nakada

---

<div class="post-metadata">

**Author:** ![Erik\_Bagfors](https://avatars.discourse-cdn.com/v4/letter/e/4da419/32.png) [@Erik\_Bagfors](https://rubytalk.org/u/Erik_Bagfors)\
**Post date:** [5 August 2002 13:49 UTC](https://rubytalk.org/t/how-to-load-script-from-a-c-extension/973/14 "2002-08-05T13:49:38Z")

</div>

Why go ruby-\>C-\>ruby? Why not do

1. parse the file
2. require your extension
3. send the parsed data to extension?

/Erik

> **···**
>
> On Mon, 2002-08-05 at 15:18, William Djaja Tjokroaminata wrote:
> 
> > Hi,
> > 
> > Yes, in my application, initially it is a Ruby script that calls my C  
> > extension, as a user will run it as
> > 
> > ```
> > ruby my_script.rb
> > 
> > ```
> > 
> > and in “my\_script.rb” the user has to put “require  
> > ‘my\_extension’”. However, the configuration file is written in Ruby,  
> > which is invoked by my\_extension. It is this part which is not working.
> > 
> > Is there another solution to this ruby-\>C-\>ruby file sequence problem?
> 
> –  
> Erik Bågfors | erik@bagfors.nu  
> Supporter of free software | GSM +46 733 279 273  
> fingerprint: 6666 A85B 95D3 D26B 296B 6C60 4F32 2C0B 693D 6E32

---

<div class="post-metadata">

**Author:** ![William\_Djaja\_Tjokro](https://avatars.discourse-cdn.com/v4/letter/w/c89c15/32.png) [@William\_Djaja\_Tjokro](https://rubytalk.org/u/William_Djaja_Tjokro)\
**Post date:** [5 August 2002 15:39 UTC](https://rubytalk.org/t/how-to-load-script-from-a-c-extension/973/15 "2002-08-05T15:39:10Z")

</div>

Hi Guy,

Thanks for the assertion. After reading the code again, it is the same  
old problem: some of the global variables have not been taken care of by  
either “rb\_global\_variable”, “rb\_define\_readonly\_variable”, or  
“rb\_define\_variable”.

The only tricky part is, the code has been working for so long, and only  
much later that the problem appears. Now I am wondering, what is the rule  
to protect the global variables? Do ALL C VALUE global variables have to  
be protected as above? Or, ALL, except:

```
- variables that have been used in "rb_define_module..."
- variables that have been used in "rb_define_class..."
- (others?)

```

On the other hand, if we protect such variables, will it result in  
segmentation fault instead? Or is it OK to “overprotect” them? (Is it  
similar to the C’s problem of is it OK to free a variable that has been  
freed?)

In the pick-axe book such variables are not protected. Currently I don’t  
protect them either. However, because of these two experiences, I don’t  
know whether I have done the right thing or not, because the problem may  
appear in the future, when I just add another function call, which will  
lead me to believe that the problem occurs because of the newly added  
code. Thanks.

Regards,

Bill

> **···**
>
> ============================================================================  
> ts [decoux@moulon.inra.fr](mailto:decoux@moulon.inra.fr) wrote:
> 
> > You have a bug. Post your source or nobody will be able to help you
> 
> > Guy Decoux

---

<div class="post-metadata">

**Author:** ![William\_Djaja\_Tjokro](https://avatars.discourse-cdn.com/v4/letter/w/c89c15/32.png) [@William\_Djaja\_Tjokro](https://rubytalk.org/u/William_Djaja_Tjokro)\
**Post date:** [5 August 2002 14:19 UTC](https://rubytalk.org/t/how-to-load-script-from-a-c-extension/973/16 "2002-08-05T14:19:01Z")

</div>

Hi,

I am not too clear regarding the meaning of “send the parsed data to  
extension”.

In my case, the extension has the default values while the file has the  
user-definable parameters. For user’s ease-of-use, he needs to do only  
“require ‘my\_extension’”. my\_extension defines several global  
(configuration) parameters with their default values. The user, however,  
can also change the parameters in a separate file called conf.rb, which  
can simply contain statements such as

```
$param1 = myValue

```

Therefore, in my\_extension, first the global variables are defined with  
their default values, and then, it may be overridden by the Ruby  
statements in conf.rb.

Also, I remember that in Tcl, there is a “source” statement which is  
pretty similar to C’s #include directive, which has the net effect of  
inserting the content of the file at that point. Is Ruby’s  
“rb\_require” or “rb\_load” the same as Tcl’s “source”?

Regards,

Bill

> **···**
>
> ========================================================================  
> Erik Bagfors [erik@bagfors.nu](mailto:erik@bagfors.nu) wrote:
> 
> > Why go ruby-\>C-\>ruby? Why not do
> > 
> > 1. parse the file
> > 2. require your extension
> > 3. send the parsed data to extension?
> 
> > /Erik

---

<div class="post-metadata">

**Author:** ![ts1](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@ts1](https://rubytalk.org/u/ts1)\
**Post date:** [5 August 2002 15:42 UTC](https://rubytalk.org/t/how-to-load-script-from-a-c-extension/973/17 "2002-08-05T15:42:59Z")

</div>

> The only tricky part is, the code has been working for so long, and only  
> much later that the problem appears. Now I am wondering, what is the rule  
> to protect the global variables? Do ALL C VALUE global variables have to  
> be protected as above? Or, ALL, except:

You really don't want to post your source 🙂

Just the part between 'Init\_xxx()' and the line where it segfault, or send  
me the source in private email

Guy Decoux

---

<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:** [5 August 2002 18:08 UTC](https://rubytalk.org/t/how-to-load-script-from-a-c-extension/973/18 "2002-08-05T18:08:13Z")

</div>

Hi,

> Thanks for the assertion. After reading the code again, it is the same  
> old problem: some of the global variables have not been taken care of by  
> either “rb\_global\_variable”, “rb\_define\_readonly\_variable”, or  
> “rb\_define\_variable”.
> 
> The only tricky part is, the code has been working for so long, and only  
> much later that the problem appears. Now I am wondering, what is the rule  
> to protect the global variables? Do ALL C VALUE global variables have to  
> be protected as above? Or, ALL, except:
> 
> ```
> - variables that have been used in "rb_define_module..."
> - variables that have been used in "rb_define_class..."
> - (others?)
> 
> ```

All of your list protect the variables. Particularly, all what  
rb\_global\_variable() does is it. And other Ruby global  
variables and classes/modules are also protected of course.

> On the other hand, if we protect such variables, will it result in  
> segmentation fault instead? Or is it OK to “overprotect” them? (Is it  
> similar to the C’s problem of is it OK to free a variable that has been  
> freed?)

Only speed and space penalties.

> **···**
>
> At Tue, 6 Aug 2002 00:39:10 +0900, William Djaja Tjokroaminata wrote:
> 
> –  
> Nobu Nakada

---

<div class="post-metadata">

**Author:** ![William\_Djaja\_Tjokr1](https://avatars.discourse-cdn.com/v4/letter/w/87869e/32.png) [@William\_Djaja\_Tjokr1](https://rubytalk.org/u/William_Djaja_Tjokr1)\
**Post date:** [5 August 2002 17:19 UTC](https://rubytalk.org/t/how-to-load-script-from-a-c-extension/973/19 "2002-08-05T17:19:16Z")

</div>

Hi Guy,

When I protect the global variables, the problem has been solved. One  
problem with the source is it is rather long (currently 2362 lines); I  
have tried to cut it, but then depending where I cut it, segmentation  
fault either appeared in different places or not at all.

Although currently the problem has been solved, I don’t know whether there  
are other “time bombs” still lurking in my code. That’s why I am asking  
the rule of when it is necessary to protect the global variables. Is it  
that such a rule does not exist that you need to see the actual source?

Regards,

Bill

> **···**
>
> ============================================================================  
> ts [decoux@moulon.inra.fr](mailto:decoux@moulon.inra.fr) wrote:
> 
> > You really don’t want to post your source 🙂
> 
> > Just the part between ‘Init\_xxx()’ and the line where it segfault, or send  
> > me the source in private email
> 
> > Guy Decoux

---

<div class="post-metadata">

**Author:** ![ts1](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@ts1](https://rubytalk.org/u/ts1)\
**Post date:** [6 August 2002 08:40 UTC](https://rubytalk.org/t/how-to-load-script-from-a-c-extension/973/20 "2002-08-06T08:40:58Z")

</div>

> Although currently the problem has been solved, I don't know whether there  
> are other "time bombs" still lurking in my code. That's why I am asking  
> the rule of when it is necessary to protect the global variables. Is it  
> that such a rule does not exist that you need to see the actual source?

No, these rules exist but I don't want to formalize them because I don't  
speak english. When you ask for help, it's best to give the maximum of  
informations, otherwise you'll have only useless responses.

Guy Decoux
