sketchucation logo sketchucation
    • Login
    ℹ️ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info

    [CExt] C array to Ruby Array?

    Scheduled Pinned Locked Moved Developers' Forum
    4 Posts 3 Posters 209 Views 3 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • thomthomT Offline
      thomthom
      last edited by

      Say you have a C array, int carr[10]; or double carr[10];.
      Is there a single method to convert the C array to a Ruby array?

      Or does one have to create a Ruby array, iterate the C array and populate one value at a time?

      Thomas Thomassen — SketchUp Monkey & Coding addict
      List of my plugins and link to the CookieWare fund

      1 Reply Last reply Reply Quote 0
      • M Offline
        MartinRinehart
        last edited by

        Ruby has its wonderful numbers (integers when possible, floats as needed) but they aren't C's much simpler (and faster) plain numbers. Cs numbers can be used directly by the CPU/FPU. Ruby's numbers are object instances.

        Author, Edges to Rubies - The Complete SketchUp Tutorial at http://www.MartinRinehart.com/models/tutorial.

        1 Reply Last reply Reply Quote 0
        • thomthomT Offline
          thomthom
          last edited by

          I am writing a Ruby C Extension. What I tried to hint by [CExt].

          TDB let me know I need to do the latter in order to convert C arrays to Ruby arrays.

          Thomas Thomassen — SketchUp Monkey & Coding addict
          List of my plugins and link to the CookieWare fund

          1 Reply Last reply Reply Quote 0
          • AdamBA Offline
            AdamB
            last edited by

            Generally No, because the underlying representations are different. (eg Integer in Ruby is actually an integer times 4)

            Doubles are the exception as the representation in C and Ruby are the same IEEE format.

            So the values can simply be copied. However the container is another matter; in C, arrays are little more than syntactic sugar, in Ruby an Array is a real live object, so you're kinda out luck.

            The best you could do would be something like:

            
            extern double *reals
            extern int rcount;
            
            static VALUE
            method_getRealArray(VALUE rarray)
            {
              if (rcount < RARRAY_LEN(rarray))
              {
                memcpy(RARRAY_PTR(rarray), reals, sizeof(double)* rcount);  
                return Qtrue;
             }
              else
              {
                return Qfalse;
              } 
            }
            
            

            Developer of LightUp Click for website

            1 Reply Last reply Reply Quote 0
            • 1 / 1
            • First post
              Last post
            Buy SketchPlus
            Buy SUbD
            Buy WrapR
            Buy eBook
            Buy Modelur
            Buy Vertex Tools
            Buy SketchCuisine
            Buy FormFonts

            Advertisement