----- Original Message -----<BR>From: Nathaniel Trellice <BR>Date: Thursday, January 11, 2007 1:02 pm<BR>Subject: Sharing constants between C and Ruby code<BR>To: ruby-talk / ruby-lang.org (ruby-talk ML)<BR><BR>> Hello,<BR>> <BR>> I'm new to ruby, but fairly experienced in C. I am using Ruby on Rails<BR>> to chat to a postgres database. I have some C functions that I can run<BR>> in the database backend. I would like to use constants defined in C<BR>> header files, e.g "#define MY_CONSTANT 100" within ruby code. I <BR>> know I<BR>> can similarly define a constant in ruby, i.e. "MY_CONSTANT=100", but<BR>> given that I have quite a few of these, and synchronising the two <BR>> filescould become tricky, is there some (preferably simple) clever <BR>> way/hackthat I can define constants in a single file and use them <BR>> both in C and<BR>> in Ruby?<BR><BR>  I think the best way is the following: have allou C defines in a separate .h file (and only those). Write a smalluby script taking all the lines in your header through something like (untested):<BR><BR>open(file) do |f|<BR>  for line in f<BR>    if line =~ /#define\s+(\w+)\s+(\S+)/<BR> uts "#{$1} = #[$2}"<BR>    end<BR>  end<BR>end<BR><BR>  Use this to create a .rb file, and have it regenerated through a Makefile.<BR><BR>  Cheers,<BR><BR>      Vince