# Global rb\_funcall

**URL:** <https://rubytalk.org/t/global-rb-funcall/23604>\
**Category:** ruby-talk\
**Created:** [26 December 2005 22:29 UTC](https://rubytalk.org/t/global-rb-funcall/23604 "2005-12-26T22:29:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Simon\_Kroger](https://avatars.discourse-cdn.com/v4/letter/s/49beb7/32.png) [@Simon\_Kroger](https://rubytalk.org/u/Simon_Kroger)\
**Post date:** [26 December 2005 22:29 UTC](https://rubytalk.org/t/global-rb-funcall/23604/1 "2005-12-26T22:29:33Z")

</div>

Hi,

i'm embedding ruby into a C++, how do i call \_global\_ functions from C?

like

&nbsp;&nbsp;&nbsp;rb\_funcall2(??, rb\_intern("test"), 0, NULL);

thanks

Simon

---

<div class="post-metadata">

**Author:** ![Eero\_Saynatkari1](https://avatars.discourse-cdn.com/v4/letter/e/46a35a/32.png) [@Eero\_Saynatkari1](https://rubytalk.org/u/Eero_Saynatkari1)\
**Post date:** [26 December 2005 22:55 UTC](https://rubytalk.org/t/global-rb-funcall/23604/2 "2005-12-26T22:55:32Z")

</div>

=?ISO-8859-1?Q?Simon\_Kr=F6ger?= wrote:

> Hi,
> 
> i'm embedding ruby into a C++, how do i call \_global\_ functions from C?
> 
> like
> 
> &nbsp;&nbsp;&nbsp;rb\_funcall2(??, rb\_intern("test"), 0, NULL);
> 
> thanks

All of the global methods become singleton methods of  
the top-level Object object, so you should be able to  
refer to rb\_cObject (I am unable to test). If that  
does not work, see if README.EXT provides any clues.

> Simon

E

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Simon\_Kroger](https://avatars.discourse-cdn.com/v4/letter/s/49beb7/32.png) [@Simon\_Kroger](https://rubytalk.org/u/Simon_Kroger)\
**Post date:** [26 December 2005 23:35 UTC](https://rubytalk.org/t/global-rb-funcall/23604/3 "2005-12-26T23:35:20Z")

</div>

Eero Saynatkari wrote:

> =?ISO-8859-1?Q?Simon\_Kr=F6ger?= wrote:
> 
> > Hi,
> > 
> > i'm embedding ruby into a C++, how do i call \_global\_ functions from C?
> > 
> > like
> > 
> > &nbsp;&nbsp;rb\_funcall2(??, rb\_intern("test"), 0, NULL);
> > 
> > thanks
> 
> All of the global methods become singleton methods of  
> the top-level Object object, so you should be able to  
> refer to rb\_cObject (I am unable to test). If that  
> does not work, see if README.EXT provides any clues.
> 
> > Simon
> 
> E

I was thinking of that possibility and dismissed the thought because "thats the class, not the object..". Well, from time to time my past trips me up.

Thanks !

Simon

---

<div class="post-metadata">

**Author:** ![Nakada\_Nobuyoshi](https://avatars.discourse-cdn.com/v4/letter/n/848f3c/32.png) [@Nakada\_Nobuyoshi](https://rubytalk.org/u/Nakada_Nobuyoshi)\
**Post date:** [27 December 2005 03:50 UTC](https://rubytalk.org/t/global-rb-funcall/23604/4 "2005-12-27T03:50:36Z")

</div>

Hi,

At Tue, 27 Dec 2005 07:55:32 +0900,  
Eero Saynatkari wrote in [ruby-talk:172589]:

> \> i'm embedding ruby into a C++, how do i call \_global\_ functions from C?  
> \>  
> \> like  
> \>  
> \> rb\_funcall2(??, rb\_intern("test"), 0, NULL);  
> \>  
> \> thanks
> 
> All of the global methods become singleton methods of  
> the top-level Object object, so you should be able to  
> refer to rb\_cObject (I am unable to test). If that  
> does not work, see if README.EXT provides any clues.

They are private instance methods of Object.

&nbsp;&nbsp;$ ruby -e 'def foo;end; p Object.private\_instance\_methods.grep(/foo/)'  
&nbsp;&nbsp;["foo"]

So the receiver is not a matter,

&nbsp;&nbsp;rb\_funcall2(Qnil, rb\_intern("test"), 0, NULL);  
&nbsp;&nbsp;rb\_funcall2(INT2FIX(999), rb\_intern("test"), 0, NULL);  
&nbsp;&nbsp;rb\_funcall2(rb\_cArray, rb\_intern("test"), 0, NULL);  
&nbsp;&nbsp;rb\_funcall2(rb\_str\_new(0, 0), rb\_intern("test"), 0, NULL);  
&nbsp;&nbsp;rb\_funcall2(rb\_ary\_new(), rb\_intern("test"), 0, NULL);  
&nbsp;&nbsp;rb\_funcall2(rb\_hash\_new(), rb\_intern("test"), 0, NULL);  
&nbsp;&nbsp;rb\_funcall2(rb\_module\_new(), rb\_intern("test"), 0, NULL);

all work.

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