Professional Web Applications Themes

calling ruby methods in C - Ruby

I have a question about what is the best way to call Ruby methods which are implemented in C from C. The two basic ways I see are : 1) using one of the rb_funcall[0-9] C functions. 2) calling the actual C function. For example if I am pushing objects onto an array in my C code I could do this: VALUE arr = rb_ary_new2(some_num); VALUE some_obj = ...; rb_ary_push(arr, some_obj); or this: VALUE arr = rb_ary_new2(some_num); VALUE some_obj = ...; rb_funcall(arr, rb_intern("push"), 1, some_obj); So my main question is: should you only call the C function defined in "ruby.h" ...

  1. #1

    Default calling ruby methods in C

    I have a question about what is the best way to call Ruby methods which
    are implemented in C from C.
    The two basic ways I see are :
    1) using one of the rb_funcall[0-9] C functions.
    2) calling the actual C function.
    For example if I am pushing objects onto an array in my C code I could do
    this:

    VALUE arr = rb_ary_new2(some_num);
    VALUE some_obj = ...;
    rb_ary_push(arr, some_obj);

    or this:

    VALUE arr = rb_ary_new2(some_num);
    VALUE some_obj = ...;
    rb_funcall(arr, rb_intern("push"), 1, some_obj);

    So my main question is:
    should you only call the C function defined in "ruby.h" directly (not
    using rb_funcall) in you extensions or is it OK to call the C functions
    defined in "intern.h" directly as well?
    thoughts:
    I assume "ruby.h" and "intern.h" are seperated for a reason and you should
    limit you C code to using stuff from "ruby.h" only, ie don't include
    "intern.h" in your extensions. One reason being that if you don't use
    rb_funcall or a similar function no OO dispatch will take place. So if
    someone overrides "push" and you call push using rb_ary_push you won't be
    calling the version of "push" you should be.
    Any opinions on this?

    -Charlie



    Charles Guest

  2. #2

    Default Re: calling ruby methods in C

    On Mon, 19 Jan 2004, Charles Mills wrote:
     

    Looks like this question was mostly answered by this post
    http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/1807
    but the question still remains is it better to use rb_funcall to invoke
    the Ruby method or call the corresponding C function directly?

    -Charlie



    Charles Guest

  3. #3

    Default Re: calling ruby methods in C

    Hi,

    At Tue, 20 Jan 2004 01:53:02 +0900,
    Charles Mills wrote: 

    For an instance made in same function, direct call would be
    enough. Otherwise, for an instance given from others, you may
    want to consider the case it is a subclass.

    --
    Nobu Nakada


    nobu.nokada@softhome.net Guest

Similar Threads

  1. calling methods on deserialized objects
    By Michael in forum PERL Modules
    Replies: 4
    Last Post: September 3rd, 09:41 AM
  2. Calling methods within a cfc
    By restlessmedia in forum Macromedia ColdFusion
    Replies: 0
    Last Post: April 26th, 02:20 PM
  3. Calling ActionScript methods from javascript?
    By dforer in forum Macromedia Flash Actionscript
    Replies: 1
    Last Post: February 19th, 12:23 AM
  4. odd problem calling object methods
    By Bill in forum PHP Development
    Replies: 4
    Last Post: November 5th, 12:45 AM
  5. calling methods in libghttp.so.1
    By david farning in forum UNIX Programming
    Replies: 1
    Last Post: July 15th, 07:58 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139