converting strings in c extension

Discussion in 'Ruby' started by Niklas C., Apr 29, 2008.

  1. Niklas C.

    Niklas C. Guest

    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.
    --
    Posted via http://www.ruby-forum.com/.
     
    Niklas C., Apr 29, 2008
    #1
    1. Advertising

  2. 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.)

    --
    vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
     
    Joel VanderWerf, Apr 29, 2008
    #2
    1. Advertising

  3. Niklas C.

    Andre Nathan Guest

    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
     
    Andre Nathan, Apr 29, 2008
    #3
  4. Niklas C.

    Niklas C. Guest

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


    --
    Posted via http://www.ruby-forum.com/.
     
    Niklas C., Apr 29, 2008
    #4
  5. Niklas C.

    Adam Shelly Guest

    On Mon, Apr 28, 2008 at 10:15 PM, Niklas C. <> 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
     
    Adam Shelly, Apr 29, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Anand
    Replies:
    3
    Views:
    915
    Tim Daneliuk
    Nov 8, 2003
  2. Christian Seberino
    Replies:
    3
    Views:
    1,210
    Christian Seberino
    Feb 5, 2004
  3. Steve
    Replies:
    3
    Views:
    27,077
    Steve
    Aug 25, 2006
  4. Elhanan
    Replies:
    0
    Views:
    561
    Elhanan
    Jan 23, 2007
  5. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    801
    Malcolm
    Jun 24, 2006
Loading...

Share This Page