C
Charles Mills
I am trying to write a function in C which calls a Ruby function using
the block it was passed when called from Ruby. Below is broken way to
do it, but it explains the idea better.
$ irb
irb(main):001:0> require 'test'
=> true
irb> my_method do |r|
irb* puts r
irb> end
LocalJumpError: no block given
from (irb):4:in `each'
from (irb):4:in `my_method'
from (irb):4
irb> exit
$ more test.c
#include <ruby.h>
#include <intern.h>
VALUE
my_method(VALUE kernel)
{
VALUE ary = rb_ary_new();
VALUE str1 = rb_str_new2("hey");
VALUE str2 = rb_str_new2("hey you");
rb_ary_push(ary, str1);
rb_ary_push(ary, str2);
return rb_funcall(ary, rb_intern("each"), 0, 0);
}
void Init_test()
{
rb_define_module_function(rb_mKernel, "my_method", my_method,
0);
}
the block it was passed when called from Ruby. Below is broken way to
do it, but it explains the idea better.
$ irb
irb(main):001:0> require 'test'
=> true
irb> my_method do |r|
irb* puts r
irb> end
LocalJumpError: no block given
from (irb):4:in `each'
from (irb):4:in `my_method'
from (irb):4
irb> exit
$ more test.c
#include <ruby.h>
#include <intern.h>
VALUE
my_method(VALUE kernel)
{
VALUE ary = rb_ary_new();
VALUE str1 = rb_str_new2("hey");
VALUE str2 = rb_str_new2("hey you");
rb_ary_push(ary, str1);
rb_ary_push(ary, str2);
return rb_funcall(ary, rb_intern("each"), 0, 0);
}
void Init_test()
{
rb_define_module_function(rb_mKernel, "my_method", my_method,
0);
}