1

I rm writing a C++ extension for Ruby, and am trying to extract a Ruby string object out of a Ruby array object, and convert it to a C/C++ string object.

I have foo.cpp:

#include <iostream>
#include <ruby.h>

VALUE bar_func(VALUE self, VALUE ary){
    std::string s = StringValuePtr(rb_ary_entry(ary, 0));
    ...
    return 0;
}

extern "C" VALUE rb_cFoo;
VALUE rb_cFoo;
extern "C" void Init_foo(){
    rb_cFoo = rb_define_class("Foo", rb_cObject);
    rb_define_singleton_method(rb_cFoo, "bar", RUBY_METHOD_FUNC(bar_func), 1);
}

and test.rb:

require "foo"
Foo.bar(["Hello World!"])

When I try to compile foo.cpp, the compiler returns an error saying something like "a single term & operand needs left side value" (not exact message. translated from an error message given in another natural lauguage). It's regarding my usage of StringValuePtr. What am I doing wrong, and how can it be fixed?

2
  • reference operator & should use left-hand value, i.e. couldn't be applied to constants. Commented Apr 18, 2012 at 13:49
  • @kirilloid Is there a way to fix the code? Commented Apr 18, 2012 at 13:55

1 Answer 1

2

First of all, StringValuePtr does not return a null-terminated C string suitable for the assignment operator. You should use the StringValueCStr macro, which also takes a VALUE as parameter.

StringValueCStr is defined as follows:

#define StringValueCStr(v) rb_string_value_cstr(&(v))

The rb_string_value_cstr function signature is:

char * rb_string_value_cstr(volatile VALUE *ptr)

After macro expansion, your code should look like this:

std::string s = rb_string_value_cstr(&rb_ary_entry(ary, 0));

Your code doesn't look wrong to me. Try putting each part in separate lines to see if you can narrow down the origin of the error:

VALUE entry = rb_ary_entry(ary, 0);
char * c_str = StringValueCStr(entry);
std::string str = c_str;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.