[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Help with a C extension

Eric Merritt

10/9/2004 12:57:00 AM

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


6 Answers

Friedrich

10/9/2004 9:30:00 AM

0

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.

Eric Merritt

10/9/2004 7:02:00 PM

0

see inline ->

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

> 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


Kristof Bastiaensen

10/9/2004 7:40:00 PM

0

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

Eric Merritt

10/9/2004 9:21:00 PM

0

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


rcoder@gmail.com

10/9/2004 10:25:00 PM

0

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

Charles Mills

10/9/2004 11:26:00 PM

0

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
>
>