Embedding Ruby into C

  • Thread starter Dennis Schridde
  • Start date
D

Dennis Schridde

--nextPart37547117.XLhn08iJld
Content-Type: text/plain;
charset="utf-8"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

Hi!
I am a Ruby newb(using it since a few days and read the book just a few wee=
ks=20
ago) and would like to embed Ruby into a C program.

I read the chapter about it in the PickAxe and this Wiki article:=20
http://www.rubygarden.org/ruby?EmbedRuby

The problem is that the PickAxe does some fancy this (it can't be that=20
complicated, can it?) and the Wiki doesn't say anything definitive at all.

I'd like to get an example, that is as easy as possible, will definetely wo=
rk=20
with Ruby 1.8 and could be called a "good way".
It should define a C function, load a Ruby script which defines a Ruby=20
function, call the Ruby function from C and call the C function from Ruby.
It should explain different return/parameter types and multiple parameters.
The C program should report if the Ruby script had a bug in it.

All in complete Ruby/C files, please.

Thanks in advance,
Dennis

--nextPart37547117.XLhn08iJld
Content-Type: application/pgp-signature

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.9.18 (GNU/Linux)

iD8DBQBDHvNsNNHWXccyBJIRAopTAJ9Gkywzwe7e8JtgkTTByEzO1vII0wCgpWkO
LhgXt5+/NoR3iqXG9piO3mI=
=dtiV
-----END PGP SIGNATURE-----

--nextPart37547117.XLhn08iJld--
 
D

Dennis Schridde

--nextPart1756300.IjOIxVPVu1
Content-Type: text/plain;
charset="utf-8"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

Am Mittwoch, 7. September 2005 16:04 schrieb Dennis Schridde:
Hi!
I am a Ruby newb(using it since a few days and read the book just a few
weeks ago) and would like to embed Ruby into a C program.

I read the chapter about it in the PickAxe and this Wiki article:
http://www.rubygarden.org/ruby?EmbedRuby

The problem is that the PickAxe does some fancy this (it can't be that
complicated, can it?) and the Wiki doesn't say anything definitive at all.

I'd like to get an example, that is as easy as possible, will definetely
work with Ruby 1.8 and could be called a "good way".
It should define a C function, load a Ruby script which defines a Ruby
function, call the Ruby function from C and call the C function from Ruby.
It should explain different return/parameter types and multiple parameter= s.
The C program should report if the Ruby script had a bug in it.

All in complete Ruby/C files, please.

Thanks in advance,
Dennis
P.S. I forgot to mention that this should/will replace the above mentioned=
=20
Wiki.
So the best would be if the base example would be pretty simple and more=20
features were added to it in additional examples.

--nextPart1756300.IjOIxVPVu1
Content-Type: application/pgp-signature

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.9.18 (GNU/Linux)

iD8DBQBDHx+5NNHWXccyBJIRAsxpAKCs9+yP+HKi880vSLo/gLv7yW/dJACg4aKM
zQ0k6Z6+/PkamAjufUrC464=
=irNU
-----END PGP SIGNATURE-----

--nextPart1756300.IjOIxVPVu1--
 
B

BearItAll

Am Mittwoch, 7. September 2005 16:04 schrieb Dennis Schridde:
P.S. I forgot to mention that this should/will replace the above mentioned
Wiki.
So the best would be if the base example would be pretty simple and more
features were added to it in additional examples.

The example below is from sourceforge, but before we get that far I'll
make the the point that I believe embedding Ruby into C/C++ is the wrong
way round. I looked at it from the point of view of using my C/C++ libs in
Ruby, particularly those areas that would be long running threads, and of
cause areas of Ruby that I am not yet comfortable with so that I can use
Ruby in serious situations more quickly, at the moment I am only at the
level of using it for utilities, though most of my regular server
utilities are now written in Ruby.

I would say that using your C/C++ libs inside Ruby is the right way round.

But also, although I am including the sourceforge example because it
directly answers the question you asked, I would say that the example they
show is based on giving a traditional 'embedded' approach. But for the
particular example they give they have Ruby dealing with data then the C++
controlling the view.

That is a classic modular split that all software should take of cause,
but it begs the question, 'For two different languages, if the functions
of the two are so separate then why embed rather than simply communicate
between two modules'. I agree that there are arguments for and against,
just want to point out that the traditional embedded approach on modern
fast machines is not always necessary or the best way to solve
these problems.

Anyway the link that tells you how to do it all is,

http://metaeditor.sourceforge.net/embed
 
D

Dennis Schridde

--nextPart1387222.XDFqo5INeI
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

Am Freitag, 9. September 2005 10:26 schrieb BearItAll:
Thanks very much for you effort!
The example below is from sourceforge, but before we get that far I'll
make the the point that I believe embedding Ruby into C/C++ is the wrong
way round.
I would say that using your C/C++ libs inside Ruby is the right way round.
Well, I wanted to see if I could extend my application with scripting suppo=
rt,=20
so this is the way to go.
But also, although I am including the sourceforge example because it
directly answers the question you asked, I would say that the example they
show is based on giving a traditional 'embedded' approach. But for the
particular example they give they have Ruby dealing with data then the C++
controlling the view.
But I think this is again very complex and it uses C++, what seems to make =
it=20
relatively different from a plain C approach.
The use a baseclass for both their Ruby and C++ classes. That is simply not=
=20
possible in my app.
It uses exceptions for error handling, what is not possible with C, as far =
as=20
I know.

And it is not suited for the wiki I mentioned.
That is a classic modular split that all software should take of cause,
but it begs the question, 'For two different languages, if the functions
of the two are so separate then why embed rather than simply communicate
between two modules'. I agree that there are arguments for and against,
just want to point out that the traditional embedded approach on modern
fast machines is not always necessary or the best way to solve
these problems.

Anyway the link that tells you how to do it all is,

http://metaeditor.sourceforge.net/embed

Pseudocode of what I imagined:

C-File:
int c_function( float param1, const char * param2);
float c_global =3D 0.5f;

int main(void)
{
ruby_function( p1, p2);
}

Ruby-File:
def ruby_function( param1, param2 )
print( c_function( c_global, param2 ) )
end


Perhaps as extension to that:

C-File:
void c_callback_to_ruby( XXX ruby_function_cb )
{
ruby_function_cb( p1, p2 );
}

Ruby-File:
def ruby_function2
c_callback_to_ruby( ruby_function ) # ruby_function -> see above
end

--nextPart1387222.XDFqo5INeI
Content-Type: application/pgp-signature

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.9.18 (GNU/Linux)

iD8DBQBDIXP1NNHWXccyBJIRAgetAKCXDDqbjft1C2WGKztasiiDu6agtQCgj0Xm
X7V4drcWD6JCot9n3bRmuiU=
=Zv5L
-----END PGP SIGNATURE-----

--nextPart1387222.XDFqo5INeI--
 
F

Friedrich Dominicus

BearItAll said:
I would say that using your C/C++ libs inside Ruby is the right way
round.

Why? Both things are viable uses. one time you want to make C stuff
available for Ruby the other time you like to use the ruby
facilties. I can see a lot of good reasons using Ruby instead of C
e.g. reading config files, string intensive stuff and probably a few
others.

Regards
Friedrich
 
B

Bill Kelly

From: "Dennis Schridde said:
But I think this is again very complex and it uses C++,
what seems to make it relatively different from a plain C
approach.

I haven't compiled the following - just cobbled it together
from other code that does run. But maybe it will be of some
help?


#include "ruby.h"

static VALUE mFoo;

static VALUE foo_hello_world (VALUE self)
{
printf("Hello! I'm a C function, called from Ruby.\n");
return self;
}

int main (int argc, char **argv)
{
// Initialize Ruby.......

#ifdef _WIN32
// ruby win32 init
NtInitialize(&argc, &argv);
#endif

ruby_init();
ruby_script("embedded");


// define a new module "Foo" containing a method "hello_world"
// which calls the C function foo_hello_world()
mFoo = rb_define_module("Foo");
rb_define_module_function(mFoo, "hello_world",
RUBY_METHOD_FUNC(foo_hello_world), 0);


// load and execute a hypothetical "test.rb" which
// can do whatever it wants - including call Foo.hello_world
int status;
rb_load_protect(rb_str_new2("test.rb"), 0, &status);
if (status == 0) {
int state = ruby_exec();
}

// TODO: example calling arbitrary Ruby method from C

return 0;
}


I didn't provide an example of calling an arbitrary Ruby
method from C. I know it's easy, but I didn't have an example
handy.


Regards,

Bill
 
D

Dennis Schridde

--nextPart3192210.jG0MBPicdI
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

Am Freitag, 9. September 2005 20:44 schrieb Bill Kelly:
I haven't compiled the following - just cobbled it together
from other code that does run. But maybe it will be of some
help?


#include "ruby.h"

static VALUE mFoo;

static VALUE foo_hello_world (VALUE self)
{
printf("Hello! I'm a C function, called from Ruby.\n");
return self;
}

int main (int argc, char **argv)
{
// Initialize Ruby.......

#ifdef _WIN32
// ruby win32 init
NtInitialize(&argc, &argv);
#endif

ruby_init();
ruby_script("embedded");


// define a new module "Foo" containing a method "hello_world"
// which calls the C function foo_hello_world()
mFoo =3D rb_define_module("Foo");
rb_define_module_function(mFoo, "hello_world",
RUBY_METHOD_FUNC(foo_hello_world), 0);


// load and execute a hypothetical "test.rb" which
// can do whatever it wants - including call Foo.hello_world
int status;
rb_load_protect(rb_str_new2("test.rb"), 0, &status);
if (status =3D=3D 0) {
int state =3D ruby_exec();
}

// TODO: example calling arbitrary Ruby method from C

return 0;
}


I didn't provide an example of calling an arbitrary Ruby
method from C. I know it's easy, but I didn't have an example
handy.
Thanks!

Now there are only two questions left:
1st -- How do I call the Ruby method from C?
I know that I must call it by ID and give also a reciever... But how do I g=
et=20
them? Or is there another method, without ID and reciever needed?

2nd -- How do I callback?

Thanks,
Dennis

--nextPart3192210.jG0MBPicdI
Content-Type: application/pgp-signature

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.9.18 (GNU/Linux)

iD8DBQBDIvZCNNHWXccyBJIRAvoCAKDaJYJXkTeO6EHzGbvpL+8ux50XgQCeKkLq
KV4Nud2cPpEOGeOO9ScJFUY=
=9067
-----END PGP SIGNATURE-----

--nextPart3192210.jG0MBPicdI--
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top