Implementing method_missing in C and blocks

I'm implementing a CLR <-> Ruby bridge using Managed C++ and the Whidbey
(.NET 2.0 runtime).
There are a lot of novel things in my bridge since I completely avoid using
Reflection to invoke methods. Instead, I use the lightweight code generation
(LCG) feature the Whidbey runtime to emit IL instructions on the fly that
form "shims" that call the appropriate CLR object methods.
I'd like to provide an elegant implementation of delegate shimming as well.
So, for example I'd like to do this:
b = System::Windows::Forms::Button.new
b.Click do |object, args|
puts 'Clicked me'
end
I've encountered a blocking issue, and I'm wondering if folks on the list
might have a workaround.
I override Object.method_missing to perform symbol lookups. So far, I
handle methods, properties, and field lookups without any problems in my
shim (well avoiding boxing value types is a hard problem but that's a LONG
story ...). I need to use method_missing to deal with Events as well.
So far, I can detect that a block is present using the rb_block_given_p()
method, which returns true as expected. Now my method_missing implementation
must be able to accept arbitrary numbers of parameters since parameters 1..N
are the actual parameters of the method that's missing.
Now, you could provide a specific implementation of method_missing to
"capture" the block in the parameter b, as in this example:
def method_missing(name, &b) ...
But I can't use this to capture the block in my method_missing
implementation since I do not know how many parameters to expect.
Is there some way for me to effectively get at the Proc object for the
block from within a method_missing implementation that takes a variable
number of arguments?
Thanks
-John

Hi,

At Sat, 24 Sep 2005 12:25:55 +0900,
John Lam wrote in [ruby-talk:157383]:

Now, you could provide a specific implementation of method_missing to
"capture" the block in the parameter b, as in this example:
def method_missing(name, &b) ...

static VALUE
method_missing(int argc, VALUE *argv, VALUE self)
{
    VALUE block = rb_block_given_p() ? rb_block_proc() : Qnil;
    ...
}

···

--
Nobu Nakada

Thanks Nobu! That's exactly what I needed.
-John
static VALUE
method_missing(int argc, VALUE *argv, VALUE self)
{
VALUE block = rb_block_given_p() ? rb_block_proc() : Qnil;
...
}

···

--
Nobu Nakada