Dear All,

(I'm re-asking in a simpler way some previously asked questions)

I'm using ruby1.8 under Linux (Debian/sid x86); I compiled ruby1.8
with gcc-3.3

I have the file myscript.rb

################ file myscript.rb
# -*- ruby -*- script myscript.rb
def sayhello(name)
  $stderr.printf("hello %s\n", name)
  $stderr.flush
end

$stderr.printf("loaded myscript.rb\n")
$stderr.flush
################ end of myscript

I also have the following C program:

################ file testemb_ruby.c
// file testemb_ruby.c
#include <stdio.h>
#include <stdlib.h>
#include <ruby.h>

int
main (int argc, char **argv)
{
  VALUE res = 0;
  int state = 0;
  char *script = 0;
  char *cmd = 0;
  ruby_init ();
  ruby_init_loadpath ();	/* to find the libraries */
  if (argc > 1)
    {
      script = argv[1];
      printf ("loading ruby script %s\n", script);
      ruby_script (script);
      rb_load_file (script);
      printf ("after load_file errinfo=");
      rb_p (ruby_errinfo);
    }
  if (argc > 2)
    cmd = argv[2];
  else
    cmd = "sayhello(\"world\")";
  printf ("before eval %s\n", cmd);
  res = rb_eval_string_protect (cmd, &state);
  printf ("after eval state=%d res=", state);
  rb_p (res);
  printf ("after eval errinfo=");
  rb_p (ruby_errinfo);
  return state;
}				// end of main

// eof testemb_ruby.c
################ end of testemb_ruby.c

I'm compiling testemb_ruby.c with ruby1.8 by

cc -g -O -Wall -O -g -I /usr/local/lib/ruby/1.8/i686-linux \
 testemb_ruby.c -o testemb_ruby -L/usr/local/lib/ruby/1.8/i686-linux \
 -lruby_1.8-static -ldl -lcrypt -lm

I'm running it with testemb_ruby myscript.rb and I am getting
                                      
loading ruby script myscript.rb
after load_file errinfo=nil
before eval sayhello("world")
after eval state=6 res=nil
after eval errinfo=#<NoMethodError: (eval):9: undefined method `sayhello' for main:Object>

I have some questions:

1. why the loading of myscript.rb don't show the "loaded myscript.rb"
   message (without giving any errors)?

2. why the eval of the 'sayhello("world")' fails after having loaded
   myscript.rb which defines the sayhello function?

3. If I run irb1.8 and type load("myscript.rb") and then
   sayhello("world") it does work (by saying hello to me)

regards

-- 
Basile STARYNKEVITCH         http://starynkevitch.net/Basile/ 
email: basile<at>starynkevitch<dot>net 
aliases: basile<at>tunes<dot>org = bstarynk<at>nerim<dot>net
8, rue de la Faż∆ncerie, 92340 Bourg La Reine, France