This works in Ruby (1.9):
irb(main):002:0> class Foo; define_method(:foo) { |x, &b| p x, b }; end
=> #<Proc:0xb7cb2cf0@(irb):2 (lambda)>
irb(main):004:0> Foo.new.foo(42) { 8 }
42
but I don't see how to duplicate this behavior in C:
cout@bean:~/tmp/tmp$ cat foo.c
#include "ruby.h"
VALUE foo(VALUE yieldarg, VALUE procarg)
{
rb_p(yieldarg);
rb_p(procarg);
rb_p(rb_block_proc());
return Qnil;
}
VALUE dm(VALUE id)
{
VALUE args[] = { id };
rb_funcall2(rb_cObject, rb_intern("define_method"), 1, args);
}
void Init_foo()
{
VALUE val = INT2NUM(42);
rb_iterate(dm, ID2SYM(rb_intern("foo")), foo, val);
}
cout@bean:~/tmp/tmp$ cat test.rb
require 'foo'
foo { puts "HERE" }
cout@bean:~/tmp/tmp$ ruby1.9 test.rb
nil
42
test.rb:3:in `<main>': tried to create Proc object without a block (ArgumentError)
Is what I want possible?
Paul