# Calling class methods from C

**URL:** <https://rubytalk.org/t/calling-class-methods-from-c/51321>\
**Category:** ruby-talk\
**Created:** [22 January 2009 19:11 UTC](https://rubytalk.org/t/calling-class-methods-from-c/51321 "2009-01-22T19:11:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Robert\_Rouse](https://avatars.discourse-cdn.com/v4/letter/r/91b2a8/32.png) [@Robert\_Rouse](https://rubytalk.org/u/Robert_Rouse)\
**Post date:** [22 January 2009 19:11 UTC](https://rubytalk.org/t/calling-class-methods-from-c/51321/1 "2009-01-22T19:11:09Z")

</div>

Hello,

I'm trying to call a class method from C. I've tried all combinations of  
rb\_intern I could think of to make it work, but I've gotten nothing.

Example class

class CallTest  
def go  
(do something here)  
end

end

rb\_funcall(?, rb\_intern("go"), 0);

What goes in the ? space? I know if I use Qnil there, it will call  
global functions, but I'd prefer class methods.

Am I heading in the wrong direction?

Also, I'd prefer not to have to know the class name ahead of time if  
possible, but if I have to require that I know what it is, I can try  
passing it by name to my application.

I'm using SWIG to generate the binding.

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

---

<div class="post-metadata">

**Author:** ![Jason\_R](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@Jason\_R](https://rubytalk.org/u/Jason_R)\
**Post date:** [22 January 2009 19:23 UTC](https://rubytalk.org/t/calling-class-methods-from-c/51321/2 "2009-01-22T19:23:45Z")

</div>

From the top of my head:

rb\_funcall(rb\_const\_get("CallTest"), rb\_intern("go"), 0);

And if you're calling "global" methods, it's better to call it on  
Kernel, where the methods are defined anyway:

rb\_funcall(rb\_mKernel, rb\_intern("method\_name"), args);

Hope that helps.

Jason

> **···**
>
> On Thu, Jan 22, 2009 at 2:11 PM, Robert Rouse \<scyllinice@scyllinice.com\> wrote:
> 
> > Hello,
> > 
> > I'm trying to call a class method from C. I've tried all combinations of  
> > rb\_intern I could think of to make it work, but I've gotten nothing.
> > 
> > Example class
> > 
> > class CallTest  
> > def go  
> > (do something here)  
> > end
> > 
> > end
> > 
> > rb\_funcall(?, rb\_intern("go"), 0);
> > 
> > What goes in the ? space? I know if I use Qnil there, it will call  
> > global functions, but I'd prefer class methods.
> > 
> > Am I heading in the wrong direction?
> > 
> > Also, I'd prefer not to have to know the class name ahead of time if  
> > possible, but if I have to require that I know what it is, I can try  
> > passing it by name to my application.
> > 
> > I'm using SWIG to generate the binding.  
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Jason\_R](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@Jason\_R](https://rubytalk.org/u/Jason_R)\
**Post date:** [22 January 2009 19:28 UTC](https://rubytalk.org/t/calling-class-methods-from-c/51321/3 "2009-01-22T19:28:02Z")

</div>

Bah, saw a small issue right when I hit "Send".

My call line works for a class defined as such:

class CallTest  
&nbsp;&nbsp;&nbsp;def self.go  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(do something here)  
&nbsp;&nbsp;&nbsp;end  
end

If you need to call an instance method on an instance of the class  
CallTest, you need to of course get a hold of that instance, or make a  
new one:

VALUE obj = rb\_funcall(rb\_const\_get("CallTest"), rb\_intern("new"), 0);  
rb\_funcall(obj, rb\_intern("go"), 0);

Jason

> **···**
>
> On Thu, Jan 22, 2009 at 2:19 PM, Jason Roelofs \<jameskilton@gmail.com\> wrote:
> 
> > From the top of my head:
> > 
> > rb\_funcall(rb\_const\_get("CallTest"), rb\_intern("go"), 0);
> > 
> > And if you're calling "global" methods, it's better to call it on  
> > Kernel, where the methods are defined anyway:
> > 
> > rb\_funcall(rb\_mKernel, rb\_intern("method\_name"), args);
> > 
> > Hope that helps.
> > 
> > Jason
> > 
> > On Thu, Jan 22, 2009 at 2:11 PM, Robert Rouse \<scyllinice@scyllinice.com\> wrote:
> > 
> > > Hello,
> > > 
> > > I'm trying to call a class method from C. I've tried all combinations of  
> > > rb\_intern I could think of to make it work, but I've gotten nothing.
> > > 
> > > Example class
> > > 
> > > class CallTest  
> > > def go  
> > > (do something here)  
> > > end
> > > 
> > > end
> > > 
> > > rb\_funcall(?, rb\_intern("go"), 0);
> > > 
> > > What goes in the ? space? I know if I use Qnil there, it will call  
> > > global functions, but I'd prefer class methods.
> > > 
> > > Am I heading in the wrong direction?
> > > 
> > > Also, I'd prefer not to have to know the class name ahead of time if  
> > > possible, but if I have to require that I know what it is, I can try  
> > > passing it by name to my application.
> > > 
> > > I'm using SWIG to generate the binding.  
> > > --  
> > > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Robert\_Rouse](https://avatars.discourse-cdn.com/v4/letter/r/91b2a8/32.png) [@Robert\_Rouse](https://rubytalk.org/u/Robert_Rouse)\
**Post date:** [22 January 2009 19:34 UTC](https://rubytalk.org/t/calling-class-methods-from-c/51321/4 "2009-01-22T19:34:21Z")

</div>

Jason Roelofs wrote:

> Bah, saw a small issue right when I hit "Send".
> 
> My call line works for a class defined as such:
> 
> class CallTest  
> &nbsp;&nbsp;&nbsp;def self.go  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(do something here)  
> &nbsp;&nbsp;&nbsp;end  
> end
> 
> If you need to call an instance method on an instance of the class  
> CallTest, you need to of course get a hold of that instance, or make a  
> new one:
> 
> VALUE obj = rb\_funcall(rb\_const\_get("CallTest"), rb\_intern("new"), 0);  
> rb\_funcall(obj, rb\_intern("go"), 0);
> 
> Jason

I tried using rb\_const\_get but it doesn't appear to call the method.  
What should I use for those parameters (VALUE and ID)?

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