Using Ruby as an extension language

Discussion in 'Ruby' started by Harold Hausman, Feb 16, 2007.

  1. I'd like to use Ruby as an extension language for software written on
    a Windows-like (actually xbox360) platform in C++.

    Specifically, I'd like to be able to embed a Ruby interpreter into my
    application so that I can do the following things:
    1. Call Ruby functions from C++
    2. Call C++ functions from Ruby
    3. Exchange data between the C++ application the the Ruby interpreter.

    I have experience doing these exact things in Lua. I understand that
    doing this in Ruby is going to be more painful than doing it in Lua as
    this was one of Lua's primary goals, but what I'm hoping is that it's
    *possible* to do it in Ruby. :)

    (because we all know that writing Ruby code is so much more enjoyable
    than writing Lua code)

    So. I downloaded the Ruby source. I had to hack it up a bit because
    the xbox is slightly different than windows, but I got a red thread
    going. (passing scripts in through -e)

    Obviously booting up the interpreter from scratch each time I want to
    run some ruby code isn't going to work:
    ruby_init();
    ruby_options();
    ruby_run();

    Is there a document somewhere that describes how to properly use ruby
    as an extension language? Is the something comparable to luaL_dofile
    or luaL_dostring and lua_register?

    Am I way off base?

    1000 thanks in advance for any insight,
    -Harold
    Harold Hausman, Feb 16, 2007
    #1
    1. Advertising

  2. On 16/02/07, Harold Hausman <> wrote:
    > I'd like to use Ruby as an extension language for software written on
    > a Windows-like (actually xbox360) platform in C++.
    >
    > Specifically, I'd like to be able to embed a Ruby interpreter into my
    > application so that I can do the following things:
    > 1. Call Ruby functions from C++
    > 2. Call C++ functions from Ruby
    > 3. Exchange data between the C++ application the the Ruby interpreter.
    >
    > I have experience doing these exact things in Lua. I understand that
    > doing this in Ruby is going to be more painful than doing it in Lua as
    > this was one of Lua's primary goals, but what I'm hoping is that it's
    > *possible* to do it in Ruby. :)
    >
    > (because we all know that writing Ruby code is so much more enjoyable
    > than writing Lua code)
    >
    > So. I downloaded the Ruby source. I had to hack it up a bit because
    > the xbox is slightly different than windows, but I got a red thread
    > going. (passing scripts in through -e)
    >
    > Obviously booting up the interpreter from scratch each time I want to
    > run some ruby code isn't going to work:
    > ruby_init();
    > ruby_options();
    > ruby_run();
    >
    > Is there a document somewhere that describes how to properly use ruby
    > as an extension language? Is the something comparable to luaL_dofile
    > or luaL_dostring and lua_register?
    >
    > Am I way off base?
    >
    > 1000 thanks in advance for any insight,
    > -Harold
    >
    >


    Programming Ruby (Pragmatic Programmers) has a nice section on
    embedding Ruby. Also have a look at README.EXT in the Ruby source
    code.

    Farrel
    Farrel Lifson, Feb 16, 2007
    #2
    1. Advertising

  3. Alle venerd=EC 16 febbraio 2007, Harold Hausman ha scritto:
    > I'd like to use Ruby as an extension language for software written on
    > a Windows-like (actually xbox360) platform in C++.
    >
    > Specifically, I'd like to be able to embed a Ruby interpreter into my
    > application so that I can do the following things:
    > 1. Call Ruby functions from C++
    > 2. Call C++ functions from Ruby
    > 3. Exchange data between the C++ application the the Ruby interpreter.
    >
    > I have experience doing these exact things in Lua. I understand that
    > doing this in Ruby is going to be more painful than doing it in Lua as
    > this was one of Lua's primary goals, but what I'm hoping is that it's
    > *possible* to do it in Ruby. :)
    >
    > (because we all know that writing Ruby code is so much more enjoyable
    > than writing Lua code)
    >
    > So. I downloaded the Ruby source. I had to hack it up a bit because
    > the xbox is slightly different than windows, but I got a red thread
    > going. (passing scripts in through -e)
    >
    > Obviously booting up the interpreter from scratch each time I want to
    > run some ruby code isn't going to work:
    > ruby_init();
    > ruby_options();
    > ruby_run();
    >
    > Is there a document somewhere that describes how to properly use ruby
    > as an extension language? Is the something comparable to luaL_dofile
    > or luaL_dostring and lua_register?
    >
    > Am I way off base?
    >
    > 1000 thanks in advance for any insight,
    > -Harold


    There's been a thread on this mailing list about embedding ruby. You may be=
    =20
    interested in it. It's at=20
    http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/235460

    I hope this helps
    Stefano Crocco, Feb 16, 2007
    #3
  4. Harold Hausman, Feb 16, 2007
    #4
  5. On 2/16/07, Farrel Lifson <> wrote:
    >
    > Programming Ruby (Pragmatic Programmers) has a nice section on
    > embedding Ruby. Also have a look at README.EXT in the Ruby source
    > code.
    >


    Okay, I've read both these, and things are going nicely.
    rb_eval_string seems much happier than ruby_options/ruby_run... :)

    2 questions:

    This seems like a nice replacement for luaL_dofile:
    rb_eval_string( "$: << Dir.getwd" );
    rb_eval_string( "require '.\\main.rb'" );

    1. This appears to be working nicely, is there anything horribly wrong with it?

    And my other question comes from when I try to expose a new method to
    the running Ruby. Here are some snippets of my code:
    VALUE rb_myprint( VALUE inSelf, VALUE inString )
    {
    // snip... but do stuff here, etc....
    return Qnil;
    }

    and then later:
    rb_define_global_function( "myprint", rb_myprint, 1 );

    2. When I try to compile this, I get the following error:
    Error 2 error C2664: 'rb_define_global_function' : cannot convert
    parameter 2 from 'VALUE (__cdecl *)(VALUE,VALUE)' to 'VALUE (__cdecl
    *)(...)' d:\code\littlecoder\littlecoder.cpp 68

    I'm not familiar with the 'old-school' "C" "..." syntax, so I'm kind
    of stuck here. If someone can fill me in on how to properly
    define/declare these function pointers so I can pass them in that'd be
    sweet.

    Thanks again,
    -Harold
    Harold Hausman, Feb 17, 2007
    #5
  6. On 2/17/07, Harold Hausman <> wrote:
    > And my other question comes from when I try to expose a new method to
    > the running Ruby. Here are some snippets of my code:
    > VALUE rb_myprint( VALUE inSelf, VALUE inString )
    > {
    > // snip... but do stuff here, etc....
    > return Qnil;
    > }
    >
    > and then later:
    > rb_define_global_function( "myprint", rb_myprint, 1 );
    >
    > 2. When I try to compile this, I get the following error:
    > Error 2 error C2664: 'rb_define_global_function' : cannot convert
    > parameter 2 from 'VALUE (__cdecl *)(VALUE,VALUE)' to 'VALUE (__cdecl
    > *)(...)' d:\code\littlecoder\littlecoder.cpp 68
    >


    So, I've found *an* answer to my question, but it feels a little nuts.
    Anyone care to confirm that this is the best practice for exposing C++
    static methods to Ruby?

    typedef VALUE (*HOOK)(...);

    ///....
    // define static functions, etc.....
    ///.....

    rb_define_global_function( "myprint", reinterpret_cast<HOOK>(rb_myprint), 1 );

    Man, ugly things like reinterpret_cast make me wish I was working with
    a dynamic language... But that's the whole point of this exercise to
    begin with, isn't it? :p

    Many thanks for any input, and apologies for the answering myself noise.

    -Harold
    Harold Hausman, Feb 17, 2007
    #6
    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. Anand
    Replies:
    3
    Views:
    885
    Tim Daneliuk
    Nov 8, 2003
  2. Lennon Day-Reynolds

    Cool use of Ruby as extension language

    Lennon Day-Reynolds, Jul 28, 2004, in forum: Ruby
    Replies:
    4
    Views:
    121
    Chris Dutton
    Aug 1, 2004
  3. Nicholas
    Replies:
    3
    Views:
    380
    Ryan Davis
    Jan 28, 2007
  4. Andrew S. Townley
    Replies:
    0
    Views:
    317
    Andrew S. Townley
    Apr 13, 2009
  5. Iñaki Baz Castillo
    Replies:
    2
    Views:
    221
    Iñaki Baz Castillo
    Apr 20, 2011
Loading...

Share This Page