# Converting strings in c extension

**URL:** <https://www.ruby-forum.com/t/converting-strings-in-c-extension/136114>\
**Category:** Ruby\
**Created:** [April 29, 2008, 2:51am UTC](https://www.ruby-forum.com/t/converting-strings-in-c-extension/136114 "2008-04-29T02:51:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![niklas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/34f0e0/32.png) [@niklas](https://www.ruby-forum.com/u/niklas)\
**Post date:** [April 29, 2008, 2:51am UTC](https://www.ruby-forum.com/t/converting-strings-in-c-extension/136114/1 "2008-04-29T02:51:12Z")

</div>

While trying to bind some old C code to ruby I ran into trouble  
converting strings. The function in question requires two string  
arguments (const char\*). This is the relevant part of my binding:

…  
static VALUE rb\_ldist(VALUE s, VALUE t)  
{  
return INT2FIX(ldist(StringValuePtr(s),StringValuePtr(t)));  
}  
…

Compilation works without warnings, but when I try to use the function  
in ruby with

ldist ‘foo’,‘bar’

it gives me

can’t convert Object into String (TypeError)

Why is it an Object and not a String?

I also tried to use RSTRING(s)-\>ptr insead of StringValuePtr, but that  
gave me an invalid pointer.

Any suggestions on what to do here?

thanks in advance for any help.

---

<div class="post-metadata">

**Author:** ![niklas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/34f0e0/32.png) [@niklas](https://www.ruby-forum.com/u/niklas)\
**Post date:** [April 29, 2008, 4:31am UTC](https://www.ruby-forum.com/t/converting-strings-in-c-extension/136114/2 "2008-04-29T04:31:04Z")

</div>

Niklas C. wrote:

> While trying to bind some old C code to ruby I ran into trouble  
> converting strings. The function in question requires two string  
> arguments (const char\*). This is the relevant part of my binding:

Try adding the following lines to see what the objects really are:

> …  
> static VALUE rb\_ldist(VALUE s, VALUE t)  
> {  
> rb\_p(s);  
> rb\_p(t);  
> return INT2FIX(ldist(StringValuePtr(s),StringValuePtr(t)));  
> }  
> …

(Btw, README.EXT should mention this function. It’s very useful.)

---

<div class="post-metadata">

**Author:** ![niklas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/34f0e0/32.png) [@niklas](https://www.ruby-forum.com/u/niklas)\
**Post date:** [April 29, 2008, 5:04am UTC](https://www.ruby-forum.com/t/converting-strings-in-c-extension/136114/3 "2008-04-29T05:04:03Z")

</div>

On Tue, 2008-04-29 at 11:30 +0900, Joel VanderWerf wrote:

> (Btw, README.EXT should mention this function. It’s very useful.)

Argh. I’ve been doing

rb\_funcall(rb\_mKernel, rb\_intern(“p”), 1, obj);

all this time…

Thanks for mentioning this 🙂

Andre

---

<div class="post-metadata">

**Author:** ![niklas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/34f0e0/32.png) [@niklas](https://www.ruby-forum.com/u/niklas)\
**Post date:** [April 29, 2008, 7:15am UTC](https://www.ruby-forum.com/t/converting-strings-in-c-extension/136114/4 "2008-04-29T07:15:50Z")

</div>

Whow, that really gave me a hint!  
Doing  
ldist ‘foo’,‘bar’  
now prints:

main  
‘foo’

So for some reason main is given as the first argument. Does ruby always  
do that? doesn’t make much sense to me…

Anyway, I solved the problem by adding another (first) parameter to  
rb\_ldist, but still specifying 2 parameters down in the rb\_define\_method  
call. Seems like a dirty hack but it’s working, thank’s a lot 😉

Joel VanderWerf wrote:

> Niklas C. wrote:
> 
> > While trying to bind some old C code to ruby I ran into trouble  
> > converting strings. The function in question requires two string  
> > arguments (const char\*). This is the relevant part of my binding:
> 
> Try adding the following lines to see what the objects really are:
> 
> > …  
> > static VALUE rb\_ldist(VALUE s, VALUE t)  
> > {  
> > rb\_p(s);  
> > rb\_p(t);  
> > return INT2FIX(ldist(StringValuePtr(s),StringValuePtr(t)));  
> > }  
> > …
> 
> (Btw, README.EXT should mention this function. It’s very useful.)

---

<div class="post-metadata">

**Author:** ![niklas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/34f0e0/32.png) [@niklas](https://www.ruby-forum.com/u/niklas)\
**Post date:** [April 29, 2008, 7:38am UTC](https://www.ruby-forum.com/t/converting-strings-in-c-extension/136114/5 "2008-04-29T07:38:16Z")

</div>

On Mon, Apr 28, 2008 at 10:15 PM, Niklas C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Whow, that really gave me a hint!  
> Doing  
> ldist ‘foo’,‘bar’  
> now prints:
> 
> main  
> ‘foo’
> 
> So for some reason main is given as the first argument. Does ruby always  
> do that? doesn’t make much sense to me…

The first argument is always ‘self’ - the object that the method is  
being called on. Your function definition should always have 1 more  
parameter than rb\_define\_method specifies.

> Anyway, I solved the problem by adding another (first) parameter to  
> rb\_ldist, but still specifying 2 parameters down in the rb\_define\_method  
> call. Seems like a dirty hack but it’s working, thank’s a lot 😉

So it’s not really a dirty hack - just rename your dummy to ‘self’ and  
you are doing the right thing.

-Adam
