gsl4r
credit: C G-K
Renaming rgslffi to gsl4r. Might as well. Hopefully I won't wake up tomorrow and decide to call it Moon Child. Project page in the works http://gsl4r.rubyforge.org Git repository already available in alpha-planning stages:
git clone git://rubyforge.org/gsl4r.git
While working on rgslffiGSL4r, I needed to define a mapping for the gsl_complex struct (we'll ignore for now that gsl_complex is potentially platform dependent).
Here's my attempt:
class GSL_Complex < ::FFI::Struct
layout :dat, [:double, 2]
R = 0
I = 1
def real()
return self[:dat][R]
end
def imag()
return self[:dat][I]
end
def equals( a )
return ( a[:dat][R] == self[:dat][R] && a[:dat][I] == self[:dat][I] )
end
def set( r, i )
self[:dat][R] = r
self[:dat][I] = i
return self
end
def set_real( r )
self[:dat][R] = r
end
def set_imag( i )
self[:dat][I] = i
end
def to_s()
return "(#{self[:dat][R]},#{self[:dat][I]})"
end
end
I was stumped for a few hours on passing FFI::Structs into external routines, luckily, this post, 'Functions returning structures' (groups/google/ruby-ffi), tipped me off by showing how to return a struct by value:
From the C code:
...
typedef struct
{
int r, g, b, a;
} ALLEGRO_COLOR;
...
