June 2003 Has anyone thought of doing a binding of Metakit for the Ruby Language, see http://www.ruby-lang.org/en/?
I have started with an interface to E4Graph. I have also been able to construct a first version of an interface for Metakit to Ruby, using SWIG. This turns out to be very simple at the cost of some loss of functionality which can be recovered by adding more things to the SWIG translation file.
I have been working on Red Hat Linux 8.0 using gcc 3.2 and SWIG 1.3.16 but I expect that this can be ported fairly easily to other operating systems.
The SWIG translation file looks like this. All it needs is a local copy of mk4.h
/* File : mk4.i */ %module mk4 %{ #include "mk4.h" %} /* First attempt at mk4 interface */ %include "mk4.h"
Example Ruby code looks like this:
# test file for ruby interface for Metakit # load the package, which must have a lower case name. require 'mk4' include Mk4 puts "A first test of Metakit running via Ruby" puts "John Fletcher June 2003 [email protected]" puts "This is intended as a translation of demo.cpp" pName = C4_StringProp.new("name") pCountry = C4_StringProp.new("country") storage = C4_Storage.new("myfile.dat",1) vAddress = storage.GetAs("address[name:S,country:S]") row = C4_Row.new() # The usual ways of setting values in a row don't work for the basic version. # This has to be used instead. pName.Set(row,"John Williams") pCountry.Set(row,"UK") vAddress.Add(row) pName.Set(row,"Paco Pena") pCountry.Set(row,"Spain") vAddress.Add(row) # This has added two records. # Here is how to get data back again s1 = pName.Get(vAddress.GetAt(1)) s2 = pCountry.Get(vAddress.GetAt(1)) puts "The country of #{s1} is #{s2}" storage.Commit()
It gives this output.
A first test of Metakit running via Ruby
John Fletcher June 2003 [email protected]
This is intended as a translation of demo.cpp
The country of Paco Pena is Spain
Note that Ruby requires class names to start with an upper case letter and this adjustment has been made by SWIG. Some operators are not translated automatically. Watch for the warnings as the translation is made.
02sep03 jcw - Thanks! I've fixed up the source code (indented it) so it comes out better.