Ruby extension and static function in C++

Discussion in 'Ruby' started by yue_nicholas@hotmail.com, Aug 15, 2006.

  1. Guest

    HI,

    I am writing an extension for Ruby in C++ and as I have more and more
    functions, I need to separate them into individual C++ source for
    easier maintenance in the long run.

    From Programming Ruby, the section on Extending Ruby defines function
    as static, if I continue to use the static keyword and the functions
    are implemented in separate C++ source file, it would not be visible in
    the main ruby method registration file.

    How significant is the static keyword when defining functions for use
    as Ruby extension?

    I had a quick browse of the ruby-netcdf package and noticed that they
    do not prefix their function with the static keyword.

    Anyone has advice?

    Cheers
     
    , Aug 15, 2006
    #1
    1. Advertising

  2. On 8/14/06, <> wrote:
    > HI,
    >
    > I am writing an extension for Ruby in C++ and as I have more and more
    > functions, I need to separate them into individual C++ source for
    > easier maintenance in the long run.
    >
    > From Programming Ruby, the section on Extending Ruby defines function
    > as static, if I continue to use the static keyword and the functions
    > are implemented in separate C++ source file, it would not be visible in
    > the main ruby method registration file.
    >
    > How significant is the static keyword when defining functions for use
    > as Ruby extension?
    >
    > I had a quick browse of the ruby-netcdf package and noticed that they
    > do not prefix their function with the static keyword.
    >
    > Anyone has advice?
    >
    > Cheers
    >
    >
    >


    As a general rule, I dislike exposing C++ as a library interface. I
    prefer to write a light wrapper in C on top of the C++ functionality-
    much easier to link with other code that way because you have much
    more control over the names that the linker sees. Then, put the Ruby
    wrapper on top of the C wrapper, and all of the Ruby extension
    functions can be static, and it will all fit reasonably into one
    source file (because the extension functions are only making calls to
    another interface).

    For an example, look at the extension code in EventMachine (under
    branch version_0). The Ruby wrapper is in rubymain.cpp. The C wrapper
    is in cmain.cpp. Everything else is C++.
     
    Francis Cianfrocca, Aug 15, 2006
    #2
    1. Advertising

  3. Guest

    Francis Cianfrocca wrote:
    > For an example, look at the extension code in EventMachine (under
    > branch version_0). The Ruby wrapper is in rubymain.cpp. The C wrapper
    > is in cmain.cpp. Everything else is C++.


    Thanks.

    Looking at the code

    static VALUE t_add_oneshot_timer (VALUE self, VALUE interval)
    {
    const char *f = evma_install_oneshot_timer (FIX2INT (interval));
    if (!f || !*f)
    rb_raise (rb_eRuntimeError, "no timer");
    return rb_str_new2 (f);
    }

    I have a further question, the parameter I get in my call is an array
    e.g.

    ["pos",[ 0, 0, 0],"dir",[1, 0, 1],"custom",[ 0, 1, 1, 2, 2, 2,
    3, 1, 1]]

    they are token-value pair

    Should I process the array in cmain.cpp or rubymain.cpp

    If in cmain.cpp I would need to include "ruby.h" for the definition of
    "VALUE" right?

    Cheers
     
    , Aug 15, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Todd
    Replies:
    1
    Views:
    323
    Erik Max Francis
    Dec 23, 2005
  2. Elhanan
    Replies:
    0
    Views:
    566
    Elhanan
    Jan 23, 2007
  3. Replies:
    2
    Views:
    533
    Jonathan Mcdougall
    Aug 6, 2007
  4. Nicholas
    Replies:
    3
    Views:
    411
    Ryan Davis
    Jan 28, 2007
  5. Iñaki Baz Castillo
    Replies:
    2
    Views:
    245
    Iñaki Baz Castillo
    Apr 20, 2011
Loading...

Share This Page