Hi all,
I am proud to announce my latest project called CplusRuby.
Below I pasted it’s README for further explanation.
Alternatively read the following blog article:
http://www.ntecs.de/blog/articles/2007/09/21/cplusruby-gluing-c-and-ruby
CplusRuby - Glue C and Ruby together (OO-like)
COPYRIGHT
Copyright (c) 2007 by Michael N. ([email protected]).
All rights reserved.
LICENSE
Ruby License.
ABOUT
With CplusRuby you can define custom C structures from within Ruby
and
wrap them easily in a Ruby class. You can as well define C functions
that can access this structure easily. CplusRuby generates
setter/getter
methods for every property and wrapper methods for the C functions.
The purpose is speed! The C functions can access the C-structure,
which is much faster than accessing instance variables. Also, the C
functions can call each other directly. This is much faster than
invoking a method in Ruby. As wrappers are generated, the Ruby-side
can access all C-structures and functions as well.
I started to write CplusRuby to implement a high-performance pulsed
neural network simulator. My existing C++ implementation suffered
from - well - C++ :). This enables me to write the core algorithms
in C or C++ and do all the other non performance-critical tasks in
Ruby.
EXAMPLE
Take a look at the following example. You should also take a look
at the generated C source file (inspire.c). Note that properties
are actually members of a C-struct, not instance variables, and as
such, their access from C is very fast. As calling a method is quite
slow in Ruby, method defined in C (method_c) can be called directly
from C, which again is very fast!
# example.rb
require 'cplusruby'
class NeuralEntity < CplusRuby
property :id
end
class Neuron < NeuralEntity
property :potential, :float
property :last_spike_time, :float
property :pre_synapses, :value
method_c :stimulate, %(float at, float weight), %{
// this is C code
selfc->potential += at*weight;
}
def initialize
self.pre_synapses = []
end
end
# generate C file, compile it and load the .so
CplusRuby.evaluate("inspire.cc", "-O3", "-lstdc++")
if __FILE__ == $0
n = Neuron.new
n.id = "n1"
n.potential = 1.0
n.stimulate(1.0, 2.0)
p n.potential # => 3.0
end
END