Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Calling class methods from C

Reply
Thread Tools

Calling class methods from C

 
 
Robert Rouse
Guest
Posts: n/a
 
      01-22-2009
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/.

 
Reply With Quote
 
 
 
 
Jason Roelofs
Guest
Posts: n/a
 
      01-22-2009
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 <(E-Mail Removed)> 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/.
>
>


 
Reply With Quote
 
 
 
 
Jason Roelofs
Guest
Posts: n/a
 
      01-22-2009
Bah, saw a small issue right when I hit "Send".

My call line works for a class defined as such:

class CallTest
def self.go
(do something here)
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 <(E-Mail Removed)> 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 <(E-Mail Removed)> 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/.
>>
>>

>


 
Reply With Quote
 
Robert Rouse
Guest
Posts: n/a
 
      01-22-2009
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
> def self.go
> (do something here)
> 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/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Design question - methods calling methods Rhino Java 49 05-29-2010 03:16 AM
calling class methods from class methods, help? Oltmans Python 6 03-11-2009 07:59 PM
Is there a way to find the class methods of a class, just like'methods' finds the instance methods? Kenneth McDonald Ruby 5 09-26-2008 03:09 PM
Avoid calling non-final methods in a constructor: Applies to static methods too? Oliver Wong Java 14 06-13-2006 09:18 AM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM



Advertisments