[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Add a method as C extension to a class defined in Ruby

Iñaki Baz Castillo

4/3/2009 3:46:00 PM

Hi, in case I want to add a method (using C) for a class I've created
in pure Ruby, must I also create the class in C?

If not, how to fill MY_CLASS here?:

rb_define_method(MY_CLASS, "my_method", my_method, arg_count);

Thanks.


--=20
I=C3=B1aki Baz Castillo
<ibc@aliax.net>

4 Answers

Andre Nathan

4/3/2009 4:03:00 PM

0

On Sat, 2009-04-04 at 00:45 +0900, Iñaki Baz Castillo wrote:
> Hi, in case I want to add a method (using C) for a class I've created
> in pure Ruby, must I also create the class in C?

Something like this should work:

static VALUE
rb_foo(VALUE self)
{
rb_p(INT2FIX(42));
return Qnil;
}

void
Init_foo(void)
{
VALUE klass = rb_const_get(rb_cObject, rb_intern("Foo"));
rb_define_method(klass, "foo", rb_foo, 0);
}

Then you should be able to do

class Foo; end
require 'foo'
Foo.new.foo

HTH,
Andre


Iñaki Baz Castillo

4/3/2009 4:28:00 PM

0

2009/4/3 Andre Nathan <andre@digirati.com.br>:
> On Sat, 2009-04-04 at 00:45 +0900, I=C3=B1aki Baz Castillo wrote:
>> Hi, in case I want to add a method (using C) for a class I've created
>> in pure Ruby, must I also create the class in C?
>
> Something like this should work:
>
> static VALUE
> rb_foo(VALUE self)
> {
> =C2=A0 =C2=A0rb_p(INT2FIX(42));
> =C2=A0 =C2=A0return Qnil;
> }
>
> void
> Init_foo(void)
> {
> =C2=A0 =C2=A0VALUE klass =3D rb_const_get(rb_cObject, rb_intern("Foo"));
> =C2=A0 =C2=A0rb_define_method(klass, "foo", rb_foo, 0);
> }
>
> Then you should be able to do
>
> class Foo; end
> require 'foo'
> Foo.new.foo


Thanks a lot!

--=20
I=C3=B1aki Baz Castillo
<ibc@aliax.net>

Eric Hodel

4/3/2009 8:39:00 PM

0

On Apr 3, 2009, at 09:03, Andre Nathan wrote:
> On Sat, 2009-04-04 at 00:45 +0900, I=F1aki Baz Castillo wrote:
>> Hi, in case I want to add a method (using C) for a class I've created
>> in pure Ruby, must I also create the class in C?
>
> Something like this should work:
>
> static VALUE
> rb_foo(VALUE self)
> {
> rb_p(INT2FIX(42));
> return Qnil;
> }
>
> void
> Init_foo(void)
> {
> VALUE klass =3D rb_const_get(rb_cObject, rb_intern("Foo"));
> rb_define_method(klass, "foo", rb_foo, 0);
> }

It's better with RubyInline:

require 'rubygems'
require 'inline'

class Foo
inline :C do |builder|
builder.c <<-C
void foo() {
rb_p(INT2FIX(42));
}
C
end
end

Foo.new.foo



The Peeler

2/19/2014 7:11:00 PM

0

On Wed, 19 Feb 2014 10:41:53 -0800 (PST), Moose in Love with Nazi Scum, now
trolling as T Rex PhD, wrote:


>>> Had GB committed an atrocious error by waging war on Germany?
>>
>> The question is a complete nonsense. Easy to see that living in Canada
>>
>> you have no knowledge of what the nazism actually was.
>
> So enlighten me if you dare. You have not so far made any logical point whatsoever.

The only "point" YOU understand is the point of a Nazi dick in your filthy
Nazi mouth, Nazi-cock sucker!

--
Moose in Love with Nazi Scum about his eyeing of his schoolmates' dicks:
"I was one of the few guys in gym class to have skin intact.
Even in the '80's when I went to the gym, most guys were cut."
MID: <0fb300f4-e4ea-4d5a-b66c-28a449e25962@u7g2000yqg.googlegroups.com>