# Adding a method to String class as Ruby C extension

**URL:** <https://rubytalk.org/t/adding-a-method-to-string-class-as-ruby-c-extension/52646>\
**Category:** ruby-talk\
**Created:** [3 April 2009 15:18 UTC](https://rubytalk.org/t/adding-a-method-to-string-class-as-ruby-c-extension/52646 "2009-04-03T15:18:02Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Inaki\_Baz\_Castillo](https://avatars.discourse-cdn.com/v4/letter/i/f14d63/32.png) [@Inaki\_Baz\_Castillo](https://rubytalk.org/u/Inaki_Baz_Castillo)\
**Post date:** [3 April 2009 15:18 UTC](https://rubytalk.org/t/adding-a-method-to-string-class-as-ruby-c-extension/52646/1 "2009-04-03T15:18:02Z")

</div>

Hi, I want to add a method to String class, and I want this method to  
be defined in C.

In the C code, to add a method I must do:

&nbsp;&nbsp;rb\_define\_method(myClass, "hello", hello, arg\_count);

but I don't know how to set myClass to point to String class.

To create a new class I should do:

&nbsp;&nbsp;VALUE myClass = rb\_define\_class\_under(myModule, "MyClass", rb\_cObject);

Since rb\_cObject means Ruby Object class, could be rb\_cString what I'm  
looking for?

Thanks.

> **···**
>
> --  
> Iñaki Baz Castillo  
> \<[ibc@aliax.net](mailto:ibc@aliax.net)\>

---

<div class="post-metadata">

**Author:** ![Inaki\_Baz\_Castillo](https://avatars.discourse-cdn.com/v4/letter/i/f14d63/32.png) [@Inaki\_Baz\_Castillo](https://rubytalk.org/u/Inaki_Baz_Castillo)\
**Post date:** [3 April 2009 15:19 UTC](https://rubytalk.org/t/adding-a-method-to-string-class-as-ruby-c-extension/52646/2 "2009-04-03T15:19:38Z")

</div>

Auto reply:  
I've found the following example:  
&nbsp;&nbsp;[http://bugs.gentoo.org/attachment.cgi?id=156407](http://bugs.gentoo.org/attachment.cgi?id=156407)  
so rb\_cString seems to be what I need.

> **···**
>
> 2009/4/3 Iñaki Baz Castillo \<ibc@aliax.net\>:
> 
> > VALUE myClass = rb\_define\_class\_under(myModule, "MyClass", rb\_cObject);
> > 
> > Since rb\_cObject means Ruby Object class, could be rb\_cString what I'm  
> > looking for?
> 
> --  
> Iñaki Baz Castillo  
> \<ibc@aliax.net\>
