Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7831073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T11:33:44+00:00 2026-06-02T11:33:44+00:00

I rm writing a C++ extension for Ruby, and am trying to extract a

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T11:33:45+00:00Added an answer on June 2, 2026 at 11:33 am

    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;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a Ruby C Extension where I'm using math.h . It's being compiled
I'm writing a Python extension that runs through a Py_UNICODE array, finds specific (ASCII,
I'm writing a Ruby 1.9 C extension and I want to do the following
Im writing a Ruby extension and Im using the function Data_wrap_struct . In order
I am writing an extension method for the String class to clean up non-ASCII
I am writing an extension method for parsing JSON string for any given type.
I have written a PHP extension library in C++. I am writing the extension
I'm writing a Ruby C Extension. I will be compiling it under Windows and
I'm very new to C and I'm writing a Ruby C Extension. One of
I'm writing an extension method that takes a string of digits (ie. 4083239382) and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.