Help with a C extension

Hello all,

I am slowly working through creating a C extension for ruby. There
are a couple of methods that would fit more correctly in existing
classes (specifically String and a few others). This is very easy to
do in Ruby, however, it I can't seem to find the way to go about it in
C. Any help would be appreciated.

Thanks
Eric

···

--
I'm a programmer, I don't have to spell correctly; I just have to
spell consistently

Eric Merritt <cyberlync@gmail.com> writes:

Hello all,

I am slowly working through creating a C extension for ruby. There
are a couple of methods that would fit more correctly in existing
classes (specifically String and a few others). This is very easy to
do in Ruby, however, it I can't seem to find the way to go about it in
C. Any help would be appreciated.

I may missing something but what about
rb_define_method.

Get a handle to the String class and use rb_define_method

Regards
Friedrich

···

--
Please remove just-for-news- to reply via e-mail.

You should be able to define extension methods for built-in classes the
same way you do for your own module.

For example:

VALUE my_extension_method(VALUE klass, VALUE self) {
/* do something here... */
return Qnil;
}

void Init_my_extension() {
rb_define_method(rb_cString, "my_method_name", my_extension_method,
0);
}

···

--
Lennon
rcoder.net

see inline ->

Eric Merritt <cyberlync@gmail.com> writes:

> Hello all,
>
> I am slowly working through creating a C extension for ruby. There
> are a couple of methods that would fit more correctly in existing
> classes (specifically String and a few others). This is very easy to
> do in Ruby, however, it I can't seem to find the way to go about it in
> C. Any help would be appreciated.
I may missing something but what about
rb_define_method.

no, no. lol. I get this.

Get a handle to the String class and use rb_define_method

Thats the question, how do you get a handle to an existing class. I
see plenty of documentation on how to create a class, but not how to
get a handle on an existing class.

-eric

···

On Sat, 9 Oct 2004 18:34:42 +0900, Friedrich Dominicus <just-for-news-frido@q-software-solutions.de> wrote:

Regards
Friedrich

--
Please remove just-for-news- to reply via e-mail.

--
I'm a programmer, I don't have to spell correctly; I just have to
spell consistently

This is helpful too:
VALUE rb_path2class _((const char*));

assert(rb_cString == rb_path2class("String"));
assert(rb_cArray == rb_path2class("Array"));
assert(cMyClass == rb_path2class("MyModule::MyClass"));

-Charlie

···

On Oct 9, 2004, at 3:29 PM, rcoder wrote:

You should be able to define extension methods for built-in classes the
same way you do for your own module.

For example:

VALUE my_extension_method(VALUE klass, VALUE self) {
/* do something here... */
return Qnil;
}

void Init_my_extension() {
rb_define_method(rb_cString, "my_method_name", my_extension_method,
0);
}

--
Lennon
rcoder.net

The VALUE for String is rb_cString, and is defined in ruby.h.
If it wasn't there you could always get the class through Object:

rb_const_get(rb_cObject, rb_intern("String"));

Regards,
Kristof

···

On Sun, 10 Oct 2004 04:01:31 +0900, Eric Merritt wrote:

Get a handle to the String class and use rb_define_method

Thats the question, how do you get a handle to an existing class. I
see plenty of documentation on how to create a class, but not how to get a
handle on an existing class.

Ah! thank you so much!

···

On Sun, 10 Oct 2004 04:39:47 +0900, Kristof Bastiaensen <kristof@vleeuwen.org> wrote:

On Sun, 10 Oct 2004 04:01:31 +0900, Eric Merritt wrote:
>> Get a handle to the String class and use rb_define_method
>
> Thats the question, how do you get a handle to an existing class. I
> see plenty of documentation on how to create a class, but not how to get a
> handle on an existing class.
>

The VALUE for String is rb_cString, and is defined in ruby.h.
If it wasn't there you could always get the class through Object:

rb_const_get(rb_cObject, rb_intern("String"));

Regards,
Kristof

--
I'm a programmer, I don't have to spell correctly; I just have to
spell consistently