From: BearItAll Date: 2005-09-09T17:26:31+09:00 Subject: Re: Embedding Ruby into C On Thu, 08 Sep 2005 02:13:31 +0900, Dennis Schridde wrote: > 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 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 > 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