 |
Forums |
Admin Discussion Forums: help Start New Thread
By: Jason Roelofs
RE: Wrapping static/class methods? [ reply ] 2008-05-14 11:28
|
|
Martyn's correct. The issue is that every method invocation in Ruby includes an explicit "self" parameter. Rice has special handling for class instance methods to where the wrapping just works, but for any other method (including static class methods), you need the self or as you've found out, you'll be missing a parameter.
|
By: Martyn Garcia
RE: Wrapping static/class methods? [ reply ] 2008-05-14 05:50
|
Hi Tomy,
I believe you are running into the self implicit parameter issue. To workaround this you can use the following method(borrowed from rb++):
class Bar
{
public:
static int add3(int value);
};
int Bar::add3(int value)
{
return value + 3;
}
int wrapBar_add3(Rice::Object self, int value)
{
return Bar::add3(value);
}
Rice::Data_Type<Bar> rb_cBar;
extern "C"
void Init_foo()
{
RUBY_TRY
{
Rice::Module rb_Mfoo = Rice::define_module("Foo");
rb_cBar = Rice::define_class_under<Bar>(rb_Mfoo,"Bar")
.define_singleton_method("add3",&wrapBar_add3);
}
RUBY_CATCH
}
Martyn
|
By: Tomy Hudson
RE: Wrapping static/class methods? [ reply ] 2008-05-14 00:19
|
Well, now it seems I am having problems with static methods that take arguments:
class Bar
{
public:
static int add3(int value);
};
int Bar::add3(int value)
{
return value + 3;
}
Rice::Data_Type<Bar> rb_cBar;
extern "C"
void Init_foo()
{
RUBY_TRY
{
Rice::Module rb_Mfoo = Rice::define_module("Foo");
rb_cBar = Rice::define_class_under<Bar>(rb_Mfoo,"Bar")
.define_singleton_method("add3",Bar::add3);
}
RUBY_CATCH
}
This compiles fine. But when I try to use it:
$ irb -rfoo
irb(main):001:0> Foo::Bar.add3(4)
ArgumentError: wrong number of arguments (1 for 0)
from (irb):1:in `add3'
from (irb):1
irb(main):002:0> Foo::Bar.add3
TypeError: can't convert Class into Integer
from (irb):2:in `add3'
from (irb):2
The second attempt looks like it is trying to pass 'self' to a C++ static class method.
Am I doing something wrong? I don't see templated versions of define*method that let you specify arguments like on classes.
|
|
 |