There are some tools that help is such situation (e.g. rake-compiler
gem), but I prefer to use RubyInline
gem. It was designed to replace slow, performance critical sections of Ruby code with implementations in other languages (e.g. C is supported out of the box), but it is also used to inline code that calls external C libraries.
A RubyInline example looks as follows:
class MyClass
inline(:C) do |builder|
builder.include '<stdio.h>'
builder.c <<-END
void my_printf(char * string){
printf("%s\\n",string);
}
END
end
end
MyClass.new.my_printf("Abc")
# prints 'Abc'
The nice feature of RubyInline is that you don't have to keep separate files for C and Ruby, some basic argument conversions are supported out of the box and you don't have to write the gluing code. The bad parts are that you don't have the full control over compilation, etc.
Personally I find RubyInline a very powerful solution.