John Lam 1997 — 2026

First Drop of RbDynamicMethod

Here’s a drop of my RbDynamicMethod library. You’ll need to have some version of Visual C++ 2005 installed to compile it. I’ve supplied a Rakefile, so the build is pretty painless provided that you have cl.exe somewhere on your path.

All of the C++ code can be found in RbDynamicMethod.h. All of the Ruby code can be found in Tests\ruby_dynamic_method.rb. The documentation is in Tests\tests.rb :)

My most recent addition is a create_safe_ruby_method wrapper. It creates a method that is callable from Ruby, but wraps the entire user-defined block in an exception handler that traps nearly all1 CLR exceptions and maps them to a Ruby RuntimeError exception.

Here’s a normal create_ruby_method from the unit tests. The weird ldc_i4_4 instruction is required since this is a Ruby varargs method that has a VALUE (int, VALUE*, VALUE) signature, and I need to return a Qnil (integer value 4) from this method.

create_ruby_method('convert_clr_exception') do
  try
    ldstr    'error'
    newobj   'Exception.ctor(String)'
    throw_ex
  catch_ex   'Exception'
    call     'static ExceptionHelper.RaiseRubyException(Exception)'
  end_try
  ldc_i4_4
  ret
end

Here’s the same method via create_safe_ruby_method, also from the unit tests:

create_safe_ruby_method('catch_clr_exception') do
  ldstr    'error'
  newobj   'Exception.ctor(String)'
  throw_ex
end

Comments and suggestions would be greatly appreciated.

[1] My code catches exceptions that derive only from Exception whereas it’s possible to throw exceptions that derive from Object.

6 archived reader comments

Barry Allison (2005-12-07)

I’m getting an error on that link

File not found

Change this error message for pages not found in public/404.html

John Lam (2005-12-07)

It seems to work fine for me. Can you try again? Anyone else having a problem?

Justin (2005-12-07)

I downloaded the code and checked out the Ruby side (I”m not real intersted in the C++), but I don’t quite understand what this accomplishes. Does this create a method, from ruby, that runs in a .NET context, using the CLR? Would you mind providing a more complete example of use? It looks like a cool project and I’d love to see more!

John Lam (2005-12-07)

Justin, it does exactly what you said it does. It’s going to form the core of my Ruby <=> CLR bridge. The first rev of the bridge (which I haven’t publicly released) was written mostly in C++, which made it much more difficult to maintain, particularly as features such as the @createsaferuby_method@ became needed.

The goal is to not really expand the C++ code much more than what you currently see, and to build the rest of the bridge entirely in Ruby.

Justin (2005/12/07)

I downloaded the code and checked out the Ruby side (I"m not real intersted in the C++), but I don’t quite understand what this accomplishes. Does this create a method, from ruby, that runs in a .NET context, using the CLR? Would you mind providing a more complete example of use? It looks like a cool project and I’d love to see more!

John Lam (2005/12/07)

Justin, it does exactly what you said it does. It’s going to form the core of my Ruby &lt;=&gt; CLR bridge. The first rev of the bridge (which I haven’t publicly released) was written mostly in C++, which made it much more difficult to maintain, particularly as features such as the @create_safe_ruby_method@ became needed. The goal is to not really expand the C++ code much more than what you currently see, and to build the rest of the bridge entirely in Ruby.