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 7062379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:35:13+00:00 2026-05-28T04:35:13+00:00

I am trying to pass a ruby array from one C function to another

  • 0

I am trying to pass a ruby array from one C function to another but I keep getting the following error

error: too few arguments to function ‘c_sum’

here is my code

require 'inline'

class ArrayMath


inline do |builder|

builder.c_singleton "
    static VALUE c_sum(VALUE arr){
    double result = 0;
    long i, len = RARRAY_LEN(arr);
    VALUE *c_arr = RARRAY_PTR(arr);

    for(i=0; i<len; i++) {
      result += NUM2DBL(c_arr[i]);
    }

return rb_float_new(result);
}"

builder.c_singleton "
static VALUE c_avg(VALUE arr){
    double sum, result, len = RARRAY_LEN(arr);

    // c_sum returns a ruby float
    sum = NUM2DBL(c_sum(arr));

    result = sum / len;

    return rb_float_new(result);
}"  

end


class << self
    alias sum c_sum
    alias avg c_avg
end
end

I get the error while trying

ArrayMath.avg([2,3.4,5.24])

  • 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-05-28T04:35:13+00:00Added an answer on May 28, 2026 at 4:35 am

    I decided to change things a bit. Here is how I got things to work.

    I created a file called array_math.rb in /lib folder (I use it in a rails application) which gets initialized from application.rb with

        config.after_initialize do
          require "#{Rails.root}/lib/arr_math.rb"
        end
    

    here is array_math.rb

    class Array
    require 'inline'
    
    inline do |builder|
      builder.c <<-EOC
            static VALUE 
            rb_ary_sum(){
                double result = 0;
                long i, len     = RARRAY_LEN(self);
                VALUE *c_arr    = RARRAY_PTR(self);
    
        for(i=0; i<len; i++) {
          result += NUM2DBL(c_arr[i]);
        }
    
                return rb_float_new(result);
            }
        EOC
    
    
        builder.c <<-EOC
            static VALUE 
            rb_ary_avg(){
                double result = 0;
                double sum, len = RARRAY_LEN(self);
    
                sum = NUM2DBL( rb_ary_sum(self) );
    
                result =  sum/len;
                return rb_float_new(result);
            }           
    
    EOC
    end
    
    def sum
        rb_ary_sum
    end
    
    def avg 
        rb_ary_avg
    end 
    
    end
    

    Now we can do this:

    arr = [2,3,6.7,3.49]
    arr.sum   => 15.19 
    arr.avg   => 3.7975
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to run the following ruby code from IRB but am unable
I'm trying to pass one method to another in elisp, and then have that
I am new to Ruby and currently trying a few examples from the Ruby
im trying to pass two parameters to a function, i being an int value
I am trying to pass a member function within a class to a function
I'm new to Ruby and am trying to pass a sort_by lambda to a
I'm trying to validate the following XML against a XSD schema using Ruby. It
Im trying to pass an array that I already found by a query into
i'm trying to pass a javascript variable into the ruby on rails 「 url_for」method
I am trying to implement something like Ruby on Rail's ActionDispatch::Flash to pass messages

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.