Calling ruby function from C

I use ruby version 1.8.7.

I've got the ruby function in the file test.rb:

def work1(arr_name)
   puts "foo1"
   for name in arr_name
    @i = @i+1
    puts @i
    puts name
   end # for
end # def

I want to call it from C-code:

#include <ruby.h>

main{
ruby_init();
ruby_script("test.rb");
rb_load_file("test.rb");

VALUE query_list;
query_list = rb_ary_new();
rb_ary_push(query_list, rb_str_new2("DVD"));
rb_ary_push(query_list, rb_str_new2("CDPlayer1"));
rb_ary_push(query_list, rb_str_new2("CDPlayer2"));

rb_funcall(0,rb_intern("work1"),1,query_list);
ruby_exec();
}

When I run it I face the problem:
"undefined method 'work1' for false:FalseClass <NoMethodError>
Segmentation fault"

When I did like this

extern VALUE ruby_top_self;
main{
.....
rb_funcall(rb_top_self,rb_intern("work1"),1,query_list);
ruby_exec();
}
I've got another error:
"undefined method 'work1' for main:Object <NoMethodError>
Segmentation fault"

How to pass parameters to a ruby function from C-code?

···

--
Posted via http://www.ruby-forum.com/.

I found the solution, but it still doesn't work properly.

I use "rb_require" instead of "rb_load_file":
extern VALUE ruby_top_self;
main{
.....
rb_require(myFileName);
rb_funcall(ruby_top_self,rb_intern("work1"),1,query_list);
ruby_exec();
}

There are 3 files: test1.rb, test2.rb, test3.rb, which have the function
"work1" in them:

def work1(arr_name)
   puts "test1"
end

def work1(arr_name)
   puts "test2"
end

def work1(arr_name)
   puts "test3"
end

I run the program and choose:
test1.rb -> test1
test2.rb -> test2
test3.rb -> test3
test1.rb -> test3
test2.rb -> test3

Why doesn't "rb_require" reload rb-file?

···

--
Posted via http://www.ruby-forum.com/.

Quoting Evgenij Markin (lists@ruby-forum.com):

Why doesn't "rb_require" reload rb-file?

If you do not restart the virtual machine, it remembers which files
have already been required. Thus, if the same file is required in
various parts of your code, it is only parsed once.

Carlo

···

Subject: Re: Calling ruby function from C
  Date: mar 18 mar 14 01:33:12 +0100

--
  * Se la Strada e la sua Virtu' non fossero state messe da parte,
* K * Carlo E. Prelz - fluido@fluido.as che bisogno ci sarebbe
  * di parlare tanto di amore e di rettitudine? (Chuang-Tzu)

You can use 'load' if you need to evaluate file each time

···

On Tue, 18 Mar 2014 13:33:12 +0100 Evgenij Markin <lists@ruby-forum.com> wrote:

Why doesn't "rb_require" reload rb-file?

--
Sergey Avseyev