>> 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.
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 support,
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
relatively different from a plain C approach.
The use a baseclass for both their Ruby and C++ classes. That is simply not
possible in my app.
It uses exceptions for error handling, what is not possible with C, as far as
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,
ruby embedded into c++
Pseudocode of what I imagined:
C-File:
int c_function( float param1, const char * param2);
float c_global = 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
···
Am Freitag, 9. September 2005 10:26 schrieb BearItAll:
On Thu, 08 Sep 2005 02:13:31 +0900, Dennis Schridde wrote:
> Am Mittwoch, 7. September 2005 16:04 schrieb Dennis Schridde: