Question

How can I convert a C++ std::string object into a Ruby VALUE object?

I tried rb_str_new2(c_string), but it did not work.

I have a function

VALUE foo(){return rb_str_new2(c_string);};

and that gives an error message:

cannot convert ‘std::string {aka std::basic_string<char>}’ to ‘const char*’ for argument ‘1’ to ‘size_t strlen(const char*)’
Was it helpful?

Solution

You are passing std::string to the function, but it expects a null-terminated const char *.

The std::string::c_str() member function can be used to get one:

rb_str_new_cstr(string.c_str());
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top