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

  • Home
  • SEARCH
  • 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 8134567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T10:04:54+00:00 2026-06-06T10:04:54+00:00

arr=Array.new(3) arr[0]=5 arr[1]=3 arr[2]=2 These lines should call this function, https://github.com/ruby/ruby/blob/trunk/array.c#L568 according to this,

  • 0
arr=Array.new(3)
arr[0]=5
arr[1]=3
arr[2]=2

These lines should call this function, https://github.com/ruby/ruby/blob/trunk/array.c#L568 according to this, http://www.ruby-doc.org/core-1.9.3/Array.html

So I have added couple of lines there to display the values of array. But i didn’t get the expected result.

else {
    memfill(RARRAY_PTR(ary), len, val);
    ARY_SET_LEN(ary, len);
    int i;
    int result;
    result = 0;
    VALUE *s_arr = RARRAY_PTR(ary);
    for(i = 0; i < len; i++) {
        result = LONG2NUM(s_arr[i]);
        printf("r: %d\n",result);
    }
}

I got the result like this:

arr=Array.new(3)
arr[0]=5
arr[1]=3
arr[2]=2
r: 9
r: 9
r: 9

Why is this result? Why 9?

I have followed these to solve it:

  • How to convert ruby array to C array with RubyInline?
  • https://github.com/ruby/ruby/blob/trunk/README.EXT#L131
  • https://github.com/ruby/ruby/blob/trunk/include/ruby/ruby.h#L708
  • https://github.com/ruby/ruby/blob/trunk/include/ruby/ruby.h#L731

Can anyone please help to display/printf the value of Ruby array in C?

  • 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-06T10:04:57+00:00Added an answer on June 6, 2026 at 10:04 am

    This doesn’t make any sense:

    result = LONG2NUM(s_arr[i]);
    

    LONG2NUM is used to convert a C long int to a Ruby VALUE that holds a number; s_arr[i] is already a VALUE so you’re effectively doing things like VALUE v = LONG2NUM((VALUE)5). LONG2NUM is defined in ruby.h as a wrapper for this:

    #define LONG2NUM_internal(v) (FIXABLE(v) ? LONG2FIX(v) : rb_int2big(v))
    

    and if v fits in a VALUE without converting it to a bignum, then you end up using this:

    #define INT2FIX(i) ((VALUE)(((SIGNED_VALUE)(i))<<1 | FIXNUM_FLAG))
    

    So you’re basically doing a bunch of bit wrangling on a pointer and wondering why it always says 9; consider yourself lucky (or unlucky) that it doesn’t say segmentation fault.

    You want to convert a VALUE which holds a number to a C integer. You probably want to use FIX2LONG:

    #define FIX2LONG(x) (long)RSHIFT((SIGNED_VALUE)(x),1)
    

    or NUM2LONG:

    #define NUM2LONG_internal(x) ((long)(FIXNUM_P(x) ? FIX2LONG(x) : rb_num2long(x)))
    #ifdef __GNUC__
    #define NUM2LONG(x) \
        __extension__ ({VALUE num2long_x = (x); NUM2LONG_internal(num2long_x);})
    #else
    static inline long
    NUM2LONG(VALUE x)
    {
        return NUM2LONG_internal(x);
    }
    #endif
    

    So something like this:

    long result;
    for(i = 0; i < len; i++) {
        result = NUM2LONG(s_arr[i]);
        printf("r: %ld\n", result);
    }
    

    Note the switch to %ld since we’re using longs now and we always turn on all the picky compiler flags that complain about mismatched printf formats and arguments. Of course, this assumes that you are certain that the array contains numbers that will fit in a C long.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is the usual way for declare a Java array: int[] arr = new
Hi when I have an array in actionscript var arr : Array = new
I have an array of integers in string form: var arr = new string[]
Let's say I have array of bytes: byte[] arr = new byte[] { 0,
This is my array arr = [1,2,3,4,5,6,7,8] I want to write a method in
Lets assume we have the following array var arr = new string[] {foo,bar,jar,\r,a,b,c,\r,x,y,z,\r); Also
When performing a MemberwiseClone of an array of value types: var arr = new
I have an array that after being sorted appears like this: var arr =
Example: var arr_1:Array = new Array(); arr_1.push({sdate:2010-02-02,status:New}); arr_1.push({sdate:2010-02-03,status:New}); arr_1.push({sdate:2010-02-04,status:New}); arr_1.push({sdate:2010-02-05,status:New}); How can i change
Given following array: var arr = [undefined, undefined, 2, 5, undefined, undefined]; I'd like

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.